@@ -15,7 +15,7 @@ associating item. For example, the `is_some` method on `Option` is intrinsically
15
15
related to Options, so should be associated.
16
16
17
17
Every associated item kind comes in two varieties: definitions that contain the
18
- actual implementation and declarations that declare specifications for
18
+ actual implementation and declarations that declare signatures for
19
19
definitions.
20
20
21
21
It is the declarations that make up the contract of traits and what it available
@@ -25,20 +25,18 @@ on generic types.
25
25
26
26
* Associated functions* are [ functions] associated with a type.
27
27
28
- An * associated function declaration* declares a specification for an associated
28
+ An * associated function declaration* declares a signature for an associated
29
29
function definition. It is written as a function item, except the
30
- function block is replaced with a ` ; ` .
30
+ function body is replaced with a ` ; ` .
31
31
32
- The identifier if the name of the function. The generics declares types for
33
- usage in the rest of the function declaration. The generics, parameter list,
34
- return type, and where clause must be the same in the associated function
35
- definition.
32
+ The identifier if the name of the function. The generics, parameter list,
33
+ return type, and where clause of the associated function must be the same as the
34
+ associated function declarations's.
36
35
37
- An * associated function definiton* is written as an associated function
38
- declaration, but instead of a ` ; ` , there is a [ block] that evaluates to the
39
- return type.
36
+ An * associated function definiton* defines a function associated with another
37
+ type. It is written the same as a [ function item] .
40
38
41
- An example of a common associated function is the ` new ` function that returns
39
+ An example of a common associated function is a ` new ` function that returns
42
40
a value of the type the associated function is associated with.
43
41
44
42
``` rust
@@ -53,21 +51,30 @@ impl Struct {
53
51
}
54
52
}
55
53
}
54
+
55
+ fn main () {
56
+ let _struct = Struct :: new ();
57
+ }
56
58
```
57
59
58
60
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 ` .
61
+ called with a [ path ] that is a path to the trait appended by the name of the
62
+ trait. When this happens, it is substituted for ` <_ as Trait>::function_name ` .
61
63
62
64
``` rust
63
65
trait Num {
64
66
fn from_i32 (n : i32 ) -> Self ;
65
67
}
68
+
66
69
impl Num for f64 {
67
70
fn from_i32 (n : i32 ) -> f64 { n as f64 }
68
71
}
69
- let x : f64 = Num :: from_i32 (42 );
70
- let x : f64 = f64 :: from_i32 (42 );
72
+
73
+ // These 4 are all equivalent in this case.
74
+ let _ : f64 = Num :: from_i32 (42 );
75
+ let _ : f64 = <_ as Num >:: from_i32 (42 );
76
+ let _ : f64 = <f64 as Num >:: from_i32 (42 );
77
+ let _ : f64 = f64 :: from_i32 (42 );
71
78
```
72
79
73
80
Associated functions whose first parameter is named ` self ` are called * methods*
@@ -78,7 +85,7 @@ When the first parameter is named `self`, the following shorthands may be used.
78
85
79
86
* ` self ` -> ` self: Self `
80
87
* ` &'lifetime self ` -> ` self: &'lifetime Self `
81
- * ` &'lifetime mut self ` -> ` &'lifetime mut Self `
88
+ * ` &'lifetime mut self ` -> ` self: &'lifetime mut Self`
82
89
83
90
> Note: Lifetimes can be and usually are elided with this shorthand.
84
91
@@ -129,20 +136,20 @@ let bounding_box = circle_shape.bounding_box();
129
136
types cannot be defined in [ inherent implementations] nor can they be given a
130
137
default implementation in traits.
131
138
132
- An * associated type declaration* declares a specification for associated type
139
+ An * associated type declaration* declares a signature for associated type
133
140
definitions. It is written as ` type ` , then an [ identifier] , and
134
- finally an optional trait bounds.
141
+ finally an optional list of trait bounds.
135
142
136
143
The identifier is the name of the declared type alias. The optional trait bounds
137
144
must be fulfilled by the implementations of the type alias.
138
145
139
146
An * associated type definition* defines a type alias on another type. It is
140
147
written as ` type ` , then an [ identifier] , then an ` = ` , and finally a [ type] .
141
148
142
- If an item ` Item ` has an associated type ` Assoc ` from a trait ` Trait ` , then
149
+ If a type ` Item ` has an associated type ` Assoc ` from a trait ` Trait ` , then
143
150
` <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.
151
+ associated type definition. Furthermore, if ` Item ` is a type parameter, then
152
+ ` Item::Assoc ` can be used in type parameters.
146
153
147
154
``` rust
148
155
trait AssociatedType {
@@ -205,14 +212,14 @@ impl<T> Container for Vec<T> {
205
212
206
213
* Associated constants* are [ constants] associated with a type.
207
214
208
- An * associated constant declaration* declares a specificatoin for associated
215
+ An * associated constant declaration* declares a signature for associated
209
216
constant definitions. It is written as ` const ` , then an identifier,
210
217
then ` : ` , then a type, finished by a ` ; ` .
211
218
212
219
The identifier is the name of the constant used in the path. The type is the
213
220
type that the definition has to implement.
214
221
215
- An * associated constant definition* defines a constant associated with another
222
+ An * associated constant definition* defines a constant associated with a
216
223
type. It is written the same as a [ constant item] .
217
224
218
225
### Associated Constants Examples
@@ -269,4 +276,5 @@ fn main() {
269
276
[ constant item ] : items/constant-items.html
270
277
[ functions ] : items/functions.html
271
278
[ method call operator ] : expressions/method-call-expr.html
272
- [ block ] : expressions/block-expr.html
279
+ [ block ] : expressions/block-expr.html
280
+ [ path ] : paths.html
0 commit comments