4
4
[ implementations] . They are called this because they are defined on an associate
5
5
type &mdash ; the type in the implementation. They are a subset of the kinds of
6
6
items you can declare in a module. Specifically, there are [ associated
7
- functions] , [ associated types] , and [ associated constants] .
7
+ functions] (including methods) , [ associated types] , and [ associated constants] .
8
8
9
9
[ associated functions ] : #associated-functions-and-methods
10
10
[ associated types ] : #associated-types
@@ -14,15 +14,20 @@ Associated items are useful when the associated item logically is related to the
14
14
associating item. For example, the ` is_some ` method on ` Option ` is intrinsically
15
15
related to Options, so should be associated.
16
16
17
- Associated items are also the contract that traits have.
17
+ Every associated item kind comes in two varieties: definitions that contain the
18
+ actual implementation and declarations that declare specifications for
19
+ definitions.
20
+
21
+ It is the declarations that make up the contract of traits and what it available
22
+ on generic types.
18
23
19
24
## Associated functions and methods
20
25
21
26
* Associated functions* are [ functions] associated with a type.
22
27
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 ` ; ` .
28
+ An * associated function declaration* declares a specification for an associated
29
+ function definition. It is written as a function item, except the
30
+ function block is replaced with a ` ; ` .
26
31
27
32
The identifier if the name of the function. The generics declares types for
28
33
usage in the rest of the function declaration. The generics, parameter list,
@@ -38,7 +43,7 @@ a value of the type the associated function is associated with.
38
43
39
44
``` rust
40
45
struct Struct {
41
- field : i32 ;
46
+ field : i32
42
47
}
43
48
44
49
impl Struct {
@@ -50,9 +55,9 @@ impl Struct {
50
55
}
51
56
```
52
57
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 ` .
58
+ When the associated function is declared on a trait, the function can also be
59
+ called on the trait. When this happens, it is substituted for
60
+ ` <_ as Trait>::function_name ` .
56
61
57
62
``` rust
58
63
trait Num {
@@ -69,12 +74,13 @@ Associated functions whose first parameter is named `self` are called *methods*
69
74
and may be invoked using the [ method call operator] , for example, ` x.foo() ` , as
70
75
well as the usual function call notation.
71
76
72
- When the first parameter is named ` self ` , the following shorthands may be used:
77
+ When the first parameter is named ` self ` , the following shorthands may be used.
73
78
74
79
* ` self ` -> ` self: Self `
75
- * ` &self ` -> ` self: &Self `
76
- * ` &mut self ` -> ` &mut Self `
77
- * ` Box<self> ` -> ` self: Box<Self> `
80
+ * ` &'lifetime self ` -> ` self: &'lifetime Self `
81
+ * ` &'lifetime mut self ` -> ` &'lifetime mut Self `
82
+
83
+ > Note: Lifetimes can be and usually are elided with this shorthand.
78
84
79
85
Consider the following trait:
80
86
@@ -88,7 +94,8 @@ trait Shape {
88
94
```
89
95
90
96
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.
97
+ of this trait while the trait is in scope can have their ` draw ` and
98
+ ` bounding_box ` methods called.
92
99
93
100
``` rust
94
101
# type Surface = i32 ;
@@ -104,11 +111,11 @@ struct Circle {
104
111
105
112
impl Shape for Circle {
106
113
// ...
107
- # fn draw (& self , Surface ) -> {}
108
- # fn bounding_box (& self ) -> BoundingBox { 0i32 ; }
114
+ # fn draw (& self , _ : Surface ) {}
115
+ # fn bounding_box (& self ) -> BoundingBox { 0i32 }
109
116
}
110
117
111
- # impl Box {
118
+ # impl Circle {
112
119
# fn new () -> Circle { Circle {} }
113
120
}
114
121
@@ -122,17 +129,20 @@ let bounding_box = circle_shape.bounding_box();
122
129
types cannot be defined in [ inherent implementations] nor can they be given a
123
130
default implementation in traits.
124
131
125
- An * associated type declaration* is written as ` type ` , then an [ identifier] , and
132
+ An * associated type declaration* declares a specification for associated type
133
+ definitions. It is written as ` type ` , then an [ identifier] , and
126
134
finally an optional trait bounds.
127
135
128
136
The identifier is the name of the declared type alias. The optional trait bounds
129
137
must be fulfilled by the implementations of the type alias.
130
138
131
- An * associated type definition* is written as ` type ` , then an [ identifier ] , then
132
- an ` = ` , and finally a [ type] .
139
+ An * associated type definition* defines a type alias on another type. It is
140
+ written as ` type ` , then an [ identifier ] , then an ` = ` , and finally a [ type] .
133
141
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.
142
+ If an item ` Item ` has an associated type ` Assoc ` from a trait ` Trait ` , then
143
+ ` <Item as Trait>::Assoc ` is a type that is an alias of the type specified in the
144
+ associated type definition. Furthermore, ` Item::Assoc ` can be used in type
145
+ parameters.
136
146
137
147
``` rust
138
148
trait AssociatedType {
@@ -156,14 +166,14 @@ impl OtherStruct {
156
166
}
157
167
158
168
fn main () {
159
- // Usage of the associated type to refer to OtherStruct as Struct::Assoc
160
- let _other_struct : OtherStruct = Struct :: Assoc :: new ();
169
+ // Usage of the associated type to refer to OtherStruct as < Struct as AssociatedType> ::Assoc
170
+ let _other_struct : OtherStruct = < Struct as AssociatedType > :: Assoc :: new ();
161
171
}
162
172
```
163
173
164
174
### Associated Types Container Example
165
175
166
- Consider the following example of a ` Container ` trait. Notice how the type is
176
+ Consider the following example of a ` Container ` trait. Notice that the type is
167
177
available for use in the method signatures:
168
178
169
179
``` rust
@@ -195,14 +205,15 @@ impl<T> Container for Vec<T> {
195
205
196
206
* Associated constants* are [ constants] associated with a type.
197
207
198
- An * associated constant declaration* is written as ` const ` , then an identifier,
208
+ An * associated constant declaration* declares a specificatoin for associated
209
+ constant definitions. It is written as ` const ` , then an identifier,
199
210
then ` : ` , then a type, finished by a ` ; ` .
200
211
201
212
The identifier is the name of the constant used in the path. The type is the
202
213
type that the definition has to implement.
203
214
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 ] .
215
+ An * associated constant definition* defines a constant associated with another
216
+ type. It is written the same as a [ constant item ] .
206
217
207
218
### Associated Constants Examples
208
219
@@ -247,14 +258,15 @@ fn main() {
247
258
```
248
259
249
260
[ trait ] : items/traits.html
261
+ [ traits ] : items/traits.html
250
262
[ type aliases ] : items/type-aliases.html
251
263
[ inherent implementations ] : items/implementations.html#inherent-implementations
252
264
[ identifier ] : identifiers.html
253
265
[ trait object ] : types.html#trait-objects
254
266
[ implementations ] : items/implementations.html
255
267
[ type ] : types.html
256
- [ constants ] : items/constants .html
257
- [ constant expression ] : expressions.html# constant-expressions
268
+ [ constants ] : items/constant-items .html
269
+ [ constant item ] : items/ constant-items.html
258
270
[ functions ] : items/functions.html
259
271
[ method call operator ] : expressions/method-call-expr.html
260
272
[ block ] : expressions/block-expr.html
0 commit comments