1
1
# Associated Items
2
2
3
+ r[ items.associated]
4
+
5
+ r[ items.associated.syntax]
3
6
> ** <sup >Syntax</sup >** \
4
7
> _ AssociatedItem_ :\
5
8
>   ;  ; [ _ OuterAttribute_ ] <sup >\* </sup > (\
6
9
>   ;  ;   ;  ;   ;  ; [ _ MacroInvocationSemi_ ] \
7
10
>   ;  ;   ;  ; | ( [ _ Visibility_ ] <sup >?</sup > ( [ _ TypeAlias_ ] | [ _ ConstantItem_ ] | [ _ Function_ ] ) )\
8
11
>   ;  ; )
9
12
13
+ r[ items.associated.intro]
10
14
* Associated Items* are the items declared in [ traits] or defined in
11
15
[ implementations] . They are called this because they are defined on an associate
12
- type &mdash ; the type in the implementation. They are a subset of the kinds of
13
- items you can declare in a module. Specifically, there are [ associated
14
- functions] (including methods), [ associated types] , and [ associated constants] .
16
+ type &mdash ; the type in the implementation.
17
+
18
+ r[ items.associated.kinds]
19
+ They are a subset of the kinds of items you can declare in a module.
20
+ Specifically, there are [ associated functions] (including methods), [ associated types] , and [ associated constants] .
15
21
16
22
[ associated functions ] : #associated-functions-and-methods
17
23
[ associated types ] : #associated-types
18
24
[ associated constants ] : #associated-constants
19
25
26
+ r[ items.associated.related]
20
27
Associated items are useful when the associated item logically is related to the
21
28
associating item. For example, the ` is_some ` method on ` Option ` is intrinsically
22
29
related to Options, so should be associated.
23
30
31
+ r[ items.associated.decl-def]
24
32
Every associated item kind comes in two varieties: definitions that contain the
25
33
actual implementation and declarations that declare signatures for
26
34
definitions.
27
35
36
+ r[ items.associated.trait-items]
28
37
It is the declarations that make up the contract of traits and what is available
29
38
on generic types.
30
39
31
40
## Associated functions and methods
32
41
42
+ r[ items.associated.fn]
43
+
44
+ r[ items.associated.fn.intro]
33
45
* Associated functions* are [ functions] associated with a type.
34
46
47
+ r[ items.associated.fn.decl]
35
48
An * associated function declaration* declares a signature for an associated
36
49
function definition. It is written as a function item, except the
37
50
function body is replaced with a ` ; ` .
38
51
39
- The identifier is the name of the function. The generics, parameter list,
40
- return type, and where clause of the associated function must be the same as the
52
+ r[ items.associated.name]
53
+ The identifier is the name of the function.
54
+
55
+ r[ items.associated.same-signature]
56
+ The generics, parameter list, return type, and where clause of the associated function must be the same as the
41
57
associated function declarations's.
42
58
59
+ r[ items.associated.fn.def]
43
60
An * associated function definition* defines a function associated with another
44
61
type. It is written the same as a [ function item] .
45
62
@@ -64,6 +81,7 @@ fn main () {
64
81
}
65
82
```
66
83
84
+ r[ items.associated.fn.qualified-self]
67
85
When the associated function is declared on a trait, the function can also be
68
86
called with a [ path] that is a path to the trait appended by the name of the
69
87
trait. When this happens, it is substituted for ` <_ as Trait>::function_name ` .
@@ -86,10 +104,14 @@ let _: f64 = f64::from_i32(42);
86
104
87
105
### Methods
88
106
107
+ r[ items.associated.fn.method]
108
+
109
+ r[ items.associated.fn.method.intro]
89
110
Associated functions whose first parameter is named ` self ` are called * methods*
90
111
and may be invoked using the [ method call operator] , for example, ` x.foo() ` , as
91
112
well as the usual function call notation.
92
113
114
+ r[ items.associated.fn.method.self-ty]
93
115
If the type of the ` self ` parameter is specified, it is limited to types resolving
94
116
to one generated by the following grammar (where ` 'lt ` denotes some arbitrary
95
117
lifetime):
@@ -127,6 +149,7 @@ impl Example {
127
149
}
128
150
```
129
151
152
+ r[ associated.fn.method.self-pat-shorthands]
130
153
Shorthand syntax can be used without specifying a type, which have the
131
154
following equivalents:
132
155
@@ -138,6 +161,7 @@ Shorthand | Equivalent
138
161
139
162
> ** Note** : Lifetimes can be, and usually are, elided with this shorthand.
140
163
164
+ r[ associated.fn.method.self-pat-mut]
141
165
If the ` self ` parameter is prefixed with ` mut ` , it becomes a mutable variable,
142
166
similar to regular parameters using a ` mut ` [ identifier pattern] . For example:
143
167
@@ -189,21 +213,30 @@ let circle_shape = Circle::new();
189
213
let bounding_box = circle_shape . bounding_box ();
190
214
```
191
215
216
+ r[ items.associated.fn.params.edition2015]
192
217
> ** Edition differences** : In the 2015 edition, it is possible to declare trait
193
218
> methods with anonymous parameters (e.g. ` fn foo(u8) ` ). This is deprecated and
194
219
> an error as of the 2018 edition. All parameters must have an argument name.
195
220
196
221
#### Attributes on method parameters
197
222
223
+ r[ items.associated.fn.param-attributes]
224
+
198
225
Attributes on method parameters follow the same rules and restrictions as
199
226
[ regular function parameters] .
200
227
201
228
## Associated Types
202
229
203
- * Associated types* are [ type aliases] associated with another type. Associated
204
- types cannot be defined in [ inherent implementations] nor can they be given a
230
+ r[ items.associated.type]
231
+
232
+ r[ items.associated.type.intro]
233
+ * Associated types* are [ type aliases] associated with another type.
234
+
235
+ r[ items.associated.type.restrictions]
236
+ Associated types cannot be defined in [ inherent implementations] nor can they be given a
205
237
default implementation in traits.
206
238
239
+ r[ items.associated.type.decl]
207
240
An * associated type declaration* declares a signature for associated type
208
241
definitions. It is written in one of the following forms, where ` Assoc ` is the
209
242
name of the associated type, ` Params ` is a comma-separated list of type,
@@ -221,13 +254,21 @@ type Assoc<Params> where WhereBounds;
221
254
type Assoc<Params>: Bounds where WhereBounds;
222
255
```
223
256
224
- The identifier is the name of the declared type alias. The optional trait bounds
225
- must be fulfilled by the implementations of the type alias.
257
+ r[ items.associated.type.name]
258
+ The identifier is the name of the declared type alias.
259
+
260
+ r[ items.associated.type.impl-fulfillment]
261
+ The optional trait bounds must be fulfilled by the implementations of the type alias.
262
+
263
+ r[ items.associated.type.sized]
226
264
There is an implicit [ ` Sized ` ] bound on associated types that can be relaxed using the special ` ?Sized ` bound.
227
265
266
+ r[ items.associated.type.def]
228
267
An * associated type definition* defines a type alias for the implementation
229
- of a trait on a type. They are written similarly to an * associated type declaration* ,
230
- but cannot contain ` Bounds ` , but instead must contain a ` Type ` :
268
+ of a trait on a type
269
+
270
+ r[ items.associated.type.def.restriction]
271
+ They are written similarly to an * associated type declaration* , but cannot contain ` Bounds ` , but instead must contain a ` Type ` :
231
272
232
273
<!-- ignore: illustrative example forms -->
233
274
``` rust,ignore
@@ -237,11 +278,15 @@ type Assoc<Params> = Type where WhereBounds;
237
278
type Assoc<Params> where WhereBounds = Type; // deprecated, prefer the form above
238
279
```
239
280
281
+ r[ items.associated.type.alias]
240
282
If a type ` Item ` has an associated type ` Assoc ` from a trait ` Trait ` , then
241
283
` <Item as Trait>::Assoc ` is a type that is an alias of the type specified in the
242
- associated type definition. Furthermore, if ` Item ` is a type parameter, then
243
- ` Item::Assoc ` can be used in type parameters.
284
+ associated type definition
285
+
286
+ r[ items.associated.type.param]
287
+ Furthermore, if ` Item ` is a type parameter, then ` Item::Assoc ` can be used in type parameters.
244
288
289
+ r[ items.associated.type.generic]
245
290
Associated types may include [ generic parameters] and [ where clauses] ; these are
246
291
often referred to as * generic associated types* , or * GATs* . If the type ` Thing `
247
292
has an associated type ` Item ` from a trait ` Trait ` with the generics ` <'a> ` , the
@@ -300,7 +345,6 @@ fn borrow<'a, T: Lend>(array: &'a mut T) -> <T as Lend>::Lender<'a> {
300
345
array . lend ()
301
346
}
302
347
303
-
304
348
fn main () {
305
349
let mut array = [0usize ; 16 ];
306
350
let lender = borrow (& mut array );
@@ -352,11 +396,15 @@ Given a reference to the associated type like `<X as Example>::Output<Y>`, the a
352
396
353
397
### Required where clauses on generic associated types
354
398
399
+ r[ items.associated.type.generic-where-clause]
400
+
401
+ r[ items.associated.type.generic-where-clause.intro]
355
402
Generic associated type declarations on traits currently may require a list of
356
403
where clauses, dependent on functions in the trait and how the GAT is used. These
357
404
rules may be loosened in the future; updates can be found [ on the generic
358
405
associated types initiative repository] ( https://rust-lang.github.io/generic-associated-types-initiative/explainer/required_bounds.html ) .
359
406
407
+ r[ items.associated.type.generic-where-clause.valid-fn]
360
408
In a few words, these where clauses are required in order to maximize the allowed
361
409
definitions of the associated type in impls. To do this, any clauses that * can be
362
410
proven to hold* on functions (using the parameters of the function or trait)
@@ -373,6 +421,7 @@ In the above, on the `next` function, we can prove that `Self: 'a`, because of
373
421
the implied bounds from ` &'a mut self ` ; therefore, we must write the equivalent
374
422
bound on the GAT itself: ` where Self: 'x ` .
375
423
424
+ r[ items.associated.type.generic-where-clause.intersection]
376
425
When there are multiple functions in a trait that use the GAT, then the
377
426
* intersection* of the bounds from the different functions are used, rather than
378
427
the union.
@@ -390,6 +439,7 @@ know that `T: 'a` on `create_checker`, we do not know that on `do_check`. Howeve
390
439
if ` do_check ` was commented out, then the ` where T: 'x ` bound would be required
391
440
on ` Checker ` .
392
441
442
+ r[ items.associated.type.generic-where-clause.forward]
393
443
The bounds on associated types also propagate required where clauses.
394
444
395
445
``` rust
@@ -404,6 +454,7 @@ Here, `where Self: 'a` is required on `Item` because of `iter`. However, `Item`
404
454
is used in the bounds of ` Iterator ` , the ` where Self: 'a ` clause is also required
405
455
there.
406
456
457
+ r[ items.associated.type.generic-where-clause.static]
407
458
Finally, any explicit uses of ` 'static ` on GATs in the trait do not count towards
408
459
the required bounds.
409
460
@@ -416,18 +467,25 @@ trait StaticReturn {
416
467
417
468
## Associated Constants
418
469
470
+ r[ items.associated.const]
471
+
472
+ r[ items.associated.const.intro]
419
473
* Associated constants* are [ constants] associated with a type.
420
474
475
+ r[ items.associated.const.decl]
421
476
An * associated constant declaration* declares a signature for associated
422
477
constant definitions. It is written as ` const ` , then an identifier,
423
478
then ` : ` , then a type, finished by a ` ; ` .
424
479
480
+ r[ items.associated.const.name]
425
481
The identifier is the name of the constant used in the path. The type is the
426
482
type that the definition has to implement.
427
483
484
+ r[ items.associated.const.def]
428
485
An * associated constant definition* defines a constant associated with a
429
486
type. It is written the same as a [ constant item] .
430
487
488
+ r[ items.associated.const.eval]
431
489
Associated constant definitions undergo [ constant evaluation] only when
432
490
referenced. Further, definitions that include [ generic parameters] are
433
491
evaluated after monomorphization.
0 commit comments