11# Associated Items
22
3+ r[ items.associated]
4+
5+ r[ items.associated.syntax]
36> ** <sup >Syntax</sup >** \
47> _ AssociatedItem_ :\
58>   ;  ; [ _ OuterAttribute_ ] <sup >\* </sup > (\
69>   ;  ;   ;  ;   ;  ; [ _ MacroInvocationSemi_ ] \
710>   ;  ;   ;  ; | ( [ _ Visibility_ ] <sup >?</sup > ( [ _ TypeAlias_ ] | [ _ ConstantItem_ ] | [ _ Function_ ] ) )\
811>   ;  ; )
912
13+ r[ items.associated.intro]
1014* Associated Items* are the items declared in [ traits] or defined in
1115[ 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] .
1521
1622[ associated functions ] : #associated-functions-and-methods
1723[ associated types ] : #associated-types
1824[ associated constants ] : #associated-constants
1925
26+ r[ items.associated.related]
2027Associated items are useful when the associated item logically is related to the
2128associating item. For example, the ` is_some ` method on ` Option ` is intrinsically
2229related to Options, so should be associated.
2330
31+ r[ items.associated.decl-def]
2432Every associated item kind comes in two varieties: definitions that contain the
2533actual implementation and declarations that declare signatures for
2634definitions.
2735
36+ r[ items.associated.trait-items]
2837It is the declarations that make up the contract of traits and what is available
2938on generic types.
3039
3140## Associated functions and methods
3241
42+ r[ items.associated.fn]
43+
44+ r[ items.associated.fn.intro]
3345* Associated functions* are [ functions] associated with a type.
3446
47+ r[ items.associated.fn.decl]
3548An * associated function declaration* declares a signature for an associated
3649function definition. It is written as a function item, except the
3750function body is replaced with a ` ; ` .
3851
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.constraint]
56+ The generics, parameter list, return type, and where clause of the associated function must be the same as the
4157associated function declarations's.
4258
59+ r[ items.associated.fn.def]
4360An * associated function definition* defines a function associated with another
4461type. It is written the same as a [ function item] .
4562
@@ -64,6 +81,7 @@ fn main () {
6481}
6582```
6683
84+ r[ items.associated.fn.qualified-self]
6785When the associated function is declared on a trait, the function can also be
6886called with a [ path] that is a path to the trait appended by the name of the
6987trait. When this happens, it is substituted for ` <_ as Trait>::function_name ` .
@@ -86,10 +104,14 @@ let _: f64 = f64::from_i32(42);
86104
87105### Methods
88106
107+ r[ items.associated.fn.method]
108+
109+ r[ items.associated.fn.method.intro]
89110Associated functions whose first parameter is named ` self ` are called * methods*
90111and may be invoked using the [ method call operator] , for example, ` x.foo() ` , as
91112well as the usual function call notation.
92113
114+ r[ items.associated.fn.method.constraint]
93115If the type of the ` self ` parameter is specified, it is limited to types resolving
94116to one generated by the following grammar (where ` 'lt ` denotes some arbitrary
95117lifetime):
@@ -99,6 +121,7 @@ P = &'lt S | &'lt mut S | Box<S> | Rc<S> | Arc<S> | Pin<P>
99121S = Self | P
100122```
101123
124+ r[ items.associated.fn.method.self-ty]
102125The ` Self ` terminal in this grammar denotes a type resolving to the implementing type.
103126This can also include the contextual type alias ` Self ` , other type aliases,
104127or associated type projections resolving to the implementing type.
@@ -127,6 +150,7 @@ impl Example {
127150}
128151```
129152
153+ r[ associated.fn.method.self-pat-shorthands]
130154Shorthand syntax can be used without specifying a type, which have the
131155following equivalents:
132156
@@ -138,6 +162,7 @@ Shorthand | Equivalent
138162
139163> ** Note** : Lifetimes can be, and usually are, elided with this shorthand.
140164
165+ r[ associated.fn.method.self-pat-mut]
141166If the ` self ` parameter is prefixed with ` mut ` , it becomes a mutable variable,
142167similar to regular parameters using a ` mut ` [ identifier pattern] . For example:
143168
@@ -189,21 +214,30 @@ let circle_shape = Circle::new();
189214let bounding_box = circle_shape . bounding_box ();
190215```
191216
217+ r[ items.associated.fn.params-edition2015]
192218> ** Edition differences** : In the 2015 edition, it is possible to declare trait
193219> methods with anonymous parameters (e.g. ` fn foo(u8) ` ). This is deprecated and
194220> an error as of the 2018 edition. All parameters must have an argument name.
195221
196222#### Attributes on method parameters
197223
224+ r[ items.associated.fn.param-attributes]
225+
198226Attributes on method parameters follow the same rules and restrictions as
199227[ regular function parameters] .
200228
201229## Associated Types
202230
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
231+ r[ items.associated.type]
232+
233+ r[ items.associated.type.intro]
234+ * Associated types* are [ type aliases] associated with another type.
235+
236+ r[ items.associated.type.constraint]
237+ Associated types cannot be defined in [ inherent implementations] nor can they be given a
205238default implementation in traits.
206239
240+ r[ items.associated.type.decl]
207241An * associated type declaration* declares a signature for associated type
208242definitions. It is written in one of the following forms, where ` Assoc ` is the
209243name of the associated type, ` Params ` is a comma-separated list of type,
@@ -221,13 +255,21 @@ type Assoc<Params> where WhereBounds;
221255type Assoc<Params>: Bounds where WhereBounds;
222256```
223257
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.
258+ r[ items.associated.type.name]
259+ The identifier is the name of the declared type alias
260+
261+ r[ items.associated.type.constraint-impl]
262+ The optional trait bounds must be fulfilled by the implementations of the type alias.
263+
264+ r[ items.associated.type.sized]
226265There is an implicit [ ` Sized ` ] bound on associated types that can be relaxed using the special ` ?Sized ` bound.
227266
267+ r[ items.associated.type.def]
228268An * 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 ` :
269+ of a trait on a type
270+
271+ r[ items.associated.type.restriction-def]
272+ They are written similarly to an * associated type declaration* , but cannot contain ` Bounds ` , but instead must contain a ` Type ` :
231273
232274<!-- ignore: illustrative example forms -->
233275``` rust,ignore
@@ -237,11 +279,15 @@ type Assoc<Params> = Type where WhereBounds;
237279type Assoc<Params> where WhereBounds = Type; // deprecated, prefer the form above
238280```
239281
282+ r[ items.associated.type.alias]
240283If a type ` Item ` has an associated type ` Assoc ` from a trait ` Trait ` , then
241284` <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.
285+ associated type definition
286+
287+ r[ items.associated.type.param]
288+ Furthermore, if ` Item ` is a type parameter, then ` Item::Assoc ` can be used in type parameters.
244289
290+ r[ items.associated.type.generic]
245291Associated types may include [ generic parameters] and [ where clauses] ; these are
246292often referred to as * generic associated types* , or * GATs* . If the type ` Thing `
247293has an associated type ` Item ` from a trait ` Trait ` with the generics ` <'a> ` , the
@@ -300,7 +346,6 @@ fn borrow<'a, T: Lend>(array: &'a mut T) -> <T as Lend>::Lender<'a> {
300346 array . lend ()
301347}
302348
303-
304349fn main () {
305350 let mut array = [0usize ; 16 ];
306351 let lender = borrow (& mut array );
@@ -352,11 +397,15 @@ Given a reference to the associated type like `<X as Example>::Output<Y>`, the a
352397
353398### Required where clauses on generic associated types
354399
400+ r[ items.associated.type.generic-where-clause]
401+
402+ r[ items.associated.type.generic-where-clause.intro]
355403Generic associated type declarations on traits currently may require a list of
356404where clauses, dependent on functions in the trait and how the GAT is used. These
357405rules may be loosened in the future; updates can be found [ on the generic
358406associated types initiative repository] ( https://rust-lang.github.io/generic-associated-types-initiative/explainer/required_bounds.html ) .
359407
408+ r[ items.associated.type.generic-where-clause.constraint-valid-fn]
360409In a few words, these where clauses are required in order to maximize the allowed
361410definitions of the associated type in impls. To do this, any clauses that * can be
362411proven to hold* on functions (using the parameters of the function or trait)
@@ -373,6 +422,7 @@ In the above, on the `next` function, we can prove that `Self: 'a`, because of
373422the implied bounds from ` &'a mut self ` ; therefore, we must write the equivalent
374423bound on the GAT itself: ` where Self: 'x ` .
375424
425+ r[ items.associated.type.generic-where-clause.constraint-intersection]
376426When there are multiple functions in a trait that use the GAT, then the
377427* intersection* of the bounds from the different functions are used, rather than
378428the union.
@@ -390,6 +440,7 @@ know that `T: 'a` on `create_checker`, we do not know that on `do_check`. Howeve
390440if ` do_check ` was commented out, then the ` where T: 'x ` bound would be required
391441on ` Checker ` .
392442
443+ r[ items.associated.type.generic-where-clause.constraint-forward]
393444The bounds on associated types also propagate required where clauses.
394445
395446``` rust
@@ -404,6 +455,7 @@ Here, `where Self: 'a` is required on `Item` because of `iter`. However, `Item`
404455is used in the bounds of ` Iterator ` , the ` where Self: 'a ` clause is also required
405456there.
406457
458+ r[ items.associated.type.generic-where-clause.static]
407459Finally, any explicit uses of ` 'static ` on GATs in the trait do not count towards
408460the required bounds.
409461
@@ -416,18 +468,25 @@ trait StaticReturn {
416468
417469## Associated Constants
418470
471+ r[ items.associated.const]
472+
473+ r[ items.associated.const.intro]
419474* Associated constants* are [ constants] associated with a type.
420475
476+ r[ items.associated.const.decl]
421477An * associated constant declaration* declares a signature for associated
422478constant definitions. It is written as ` const ` , then an identifier,
423479then ` : ` , then a type, finished by a ` ; ` .
424480
481+ r[ items.associated.const.name]
425482The identifier is the name of the constant used in the path. The type is the
426483type that the definition has to implement.
427484
485+ r[ items.associated.const.def]
428486An * associated constant definition* defines a constant associated with a
429487type. It is written the same as a [ constant item] .
430488
489+ r[ items.associated.const.eval]
431490Associated constant definitions undergo [ constant evaluation] only when
432491referenced. Further, definitions that include [ generic parameters] are
433492evaluated after monomorphization.
0 commit comments