|
| 1 | +# Associated Items |
| 2 | + |
| 3 | +*Associated Items* are the items declared in [traits] or defined in |
| 4 | +[implementations]. They are called this because they are defined on an associate |
| 5 | +type — the type in the implementation. They are a subset of the kinds of |
| 6 | +items you can declare in a module. Specifically, there are [associated |
| 7 | +functions], [associated types], and [associated constants]. |
| 8 | + |
| 9 | +[associated functions]: #associated-functions-and-methods |
| 10 | +[associated types]: #associated-types |
| 11 | +[associated constants]: #associated-constants |
| 12 | + |
| 13 | +Associated items are useful when the associated item logically is related to the |
| 14 | +associating item. For example, the `is_some` method on `Option` is intrinsically |
| 15 | +related to Options, so should be associated. |
| 16 | + |
| 17 | +Associated items are also the contract that traits have. |
| 18 | + |
| 19 | +## Associated functions and methods |
| 20 | + |
| 21 | +*Associated functions* are [functions] associated with a type. |
| 22 | + |
| 23 | +An *associated function declaration* is written as `fn`, then an [identifier] |
| 24 | +then optional generics, then `(` then a parameter list, then `)`, then |
| 25 | +optionally `->` and a type, then an optional where clause, then finally a `;`. |
| 26 | + |
| 27 | +The identifier if the name of the function. The generics declares types for |
| 28 | +usage in the rest of the function declaration. The generics, parameter list, |
| 29 | +return type, and where clause must be the same in the associated function |
| 30 | +definition. |
| 31 | + |
| 32 | +An *associated function definiton* is written as an associated function |
| 33 | +declaration, but instead of a `;`, there is a [block] that evaluates to the |
| 34 | +return type. |
| 35 | + |
| 36 | +An example of a common associated function is the `new` function that returns |
| 37 | +a value of the type the associated function is associated with. |
| 38 | + |
| 39 | +```rust |
| 40 | +struct Struct { |
| 41 | + field: i32; |
| 42 | +} |
| 43 | + |
| 44 | +impl Struct { |
| 45 | + fn new() -> Struct { |
| 46 | + Struct { |
| 47 | + field: 0i32 |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +When the associated function is declared on a trait, the function can be called |
| 54 | +on the trait. When this happens, it is substituted for |
| 55 | +`<_ as Trait>::function_name`. |
| 56 | + |
| 57 | +```rust |
| 58 | +trait Num { |
| 59 | + fn from_i32(n: i32) -> Self; |
| 60 | +} |
| 61 | +impl Num for f64 { |
| 62 | + fn from_i32(n: i32) -> f64 { n as f64 } |
| 63 | +} |
| 64 | +let x: f64 = Num::from_i32(42); |
| 65 | +let x: f64 = f64::from_i32(42); |
| 66 | +``` |
| 67 | + |
| 68 | +Associated functions whose first parameter is named `self` are called *methods* |
| 69 | +and may be invoked using the [method call operator], for example, `x.foo()`, as |
| 70 | +well as the usual function call notation. |
| 71 | + |
| 72 | +When the first parameter is named `self`, the following shorthands may be used: |
| 73 | + |
| 74 | +* `self` -> `self: Self` |
| 75 | +* `&self` -> `self: &Self` |
| 76 | +* `&mut self` -> `&mut Self` |
| 77 | +* `Box<self>` -> `self: Box<Self>` |
| 78 | + |
| 79 | +Consider the following trait: |
| 80 | + |
| 81 | +```rust |
| 82 | +# type Surface = i32; |
| 83 | +# type BoundingBox = i32; |
| 84 | +trait Shape { |
| 85 | + fn draw(&self, Surface); |
| 86 | + fn bounding_box(&self) -> BoundingBox; |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +This defines a trait with two methods. All values that have [implementations] |
| 91 | +of this trait in scope can have their `draw` and `bounding_box` methods called. |
| 92 | + |
| 93 | +```rust |
| 94 | +# type Surface = i32; |
| 95 | +# type BoundingBox = i32; |
| 96 | +# trait Shape { |
| 97 | +# fn draw(&self, Surface); |
| 98 | +# fn bounding_box(&self) -> BoundingBox; |
| 99 | +# } |
| 100 | + |
| 101 | +struct Circle { |
| 102 | + // ... |
| 103 | +} |
| 104 | + |
| 105 | +impl Shape for Circle { |
| 106 | + // ... |
| 107 | +# fn draw(&self, Surface) -> {} |
| 108 | +# fn bounding_box(&self) -> BoundingBox { 0i32; } |
| 109 | +} |
| 110 | + |
| 111 | +# impl Box { |
| 112 | +# fn new() -> Circle { Circle{} } |
| 113 | +} |
| 114 | + |
| 115 | +let circle_shape = Circle::new(); |
| 116 | +let bounding_box = circle_shape.bounding_box(); |
| 117 | +``` |
| 118 | + |
| 119 | +## Associated Types |
| 120 | + |
| 121 | +*Associated types* are [type aliases] associated with another type. Associated |
| 122 | +types cannot be defined in [inherent implementations] nor can they be given a |
| 123 | +default implementation in traits. |
| 124 | + |
| 125 | +An *associated type declaration* is written as `type`, then an [identifier], and |
| 126 | +finally an optional trait bounds. |
| 127 | + |
| 128 | +The identifier is the name of the declared type alias. The optional trait bounds |
| 129 | +must be fulfilled by the implementations of the type alias. |
| 130 | + |
| 131 | +An *associated type definition* is written as `type`, then an [identifier], then |
| 132 | +an `=`, and finally a [type]. |
| 133 | + |
| 134 | +If an item `Item` has an associated type `Assoc`, then `Item::Assoc` is a type |
| 135 | +that is an alias of the type specified in the associated type definition. |
| 136 | + |
| 137 | +```rust |
| 138 | +trait AssociatedType { |
| 139 | + // Associated type declaration |
| 140 | + type Assoc; |
| 141 | +} |
| 142 | + |
| 143 | +struct Struct; |
| 144 | + |
| 145 | +struct OtherStruct; |
| 146 | + |
| 147 | +impl AssociatedType for Struct { |
| 148 | + // Associated type definition |
| 149 | + type Assoc = OtherStruct; |
| 150 | +} |
| 151 | + |
| 152 | +impl OtherStruct { |
| 153 | + fn new() -> OtherStruct { |
| 154 | + OtherStruct |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +fn main() { |
| 159 | + // Usage of the associated type to refer to OtherStruct as Struct::Assoc |
| 160 | + let _other_struct: OtherStruct = Struct::Assoc::new(); |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +### Associated Types Container Example |
| 165 | + |
| 166 | +Consider the following example of a `Container` trait. Notice how the type is |
| 167 | +available for use in the method signatures: |
| 168 | + |
| 169 | +```rust |
| 170 | +trait Container { |
| 171 | + type E; |
| 172 | + fn empty() -> Self; |
| 173 | + fn insert(&mut self, Self::E); |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +In order for a type to implement this trait, it must not only provide |
| 178 | +implementations for every method, but it must specify the type `E`. Here's an |
| 179 | +implementation of `Container` for the standard library type `Vec`: |
| 180 | + |
| 181 | +```rust |
| 182 | +# trait Container { |
| 183 | +# type E; |
| 184 | +# fn empty() -> Self; |
| 185 | +# fn insert(&mut self, Self::E); |
| 186 | +# } |
| 187 | +impl<T> Container for Vec<T> { |
| 188 | + type E = T; |
| 189 | + fn empty() -> Vec<T> { Vec::new() } |
| 190 | + fn insert(&mut self, x: T) { self.push(x); } |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +## Associated Constants |
| 195 | + |
| 196 | +*Associated constants* are [constants] associated with a type. |
| 197 | + |
| 198 | +An *associated constant declaration* is written as `const`, then an identifier, |
| 199 | +then `:`, then a type, finished by a `;`. |
| 200 | + |
| 201 | +The identifier is the name of the constant used in the path. The type is the |
| 202 | +type that the definition has to implement. |
| 203 | + |
| 204 | +An *associated constant definition* is written as a declaraction, but between |
| 205 | +the type and the `;`, there is an `=` followed by a [constant expression]. |
| 206 | + |
| 207 | +### Associated Constants Examples |
| 208 | + |
| 209 | +A basic example: |
| 210 | + |
| 211 | +```rust |
| 212 | +trait ConstantId { |
| 213 | + const ID: i32; |
| 214 | +} |
| 215 | + |
| 216 | +struct Struct; |
| 217 | + |
| 218 | +impl ConstantId for Struct { |
| 219 | + const ID: i32 = 1; |
| 220 | +} |
| 221 | + |
| 222 | +fn main() { |
| 223 | + assert_eq!(1, Struct::ID); |
| 224 | +} |
| 225 | +``` |
| 226 | + |
| 227 | +Using default values: |
| 228 | + |
| 229 | +```rust |
| 230 | +trait ConstantIdDefault { |
| 231 | + const ID: i32 = 1; |
| 232 | +} |
| 233 | + |
| 234 | +struct Struct; |
| 235 | +struct OtherStruct; |
| 236 | + |
| 237 | +impl ConstantIdDefault for Struct {} |
| 238 | + |
| 239 | +impl ConstantIdDefault for OtherStruct { |
| 240 | + const ID: i32 = 5; |
| 241 | +} |
| 242 | + |
| 243 | +fn main() { |
| 244 | + assert_eq!(1, Struct::ID); |
| 245 | + assert_eq!(5, OtherStruct::ID); |
| 246 | +} |
| 247 | +``` |
| 248 | + |
| 249 | +[trait]: items/traits.html |
| 250 | +[type aliases]: items/type-aliases.html |
| 251 | +[inherent implementations]: items/implementations.html#inherent-implementations |
| 252 | +[identifier]: identifiers.html |
| 253 | +[trait object]: types.html#trait-objects |
| 254 | +[implementations]: items/implementations.html |
| 255 | +[type]: types.html |
| 256 | +[constants]: items/constants.html |
| 257 | +[constant expression]: expressions.html#constant-expressions |
| 258 | +[functions]: items/functions.html |
| 259 | +[method call operator]: expressions/method-call-expr.html |
| 260 | +[block]: expressions/block-expr.html |
0 commit comments