@@ -220,7 +220,12 @@ If a type `Item` has an associated type `Assoc` from a trait `Trait`, then
220
220
associated type definition. Furthermore, if ` Item ` is a type parameter, then
221
221
` Item::Assoc ` can be used in type parameters.
222
222
223
- Associated types must not include [ generic parameters] or [ where clauses] .
223
+ Associated types may include [ generic parameters] or [ where clauses] ; these may
224
+ be referred to generic associated types, or GATs. If the type ` Thing ` has an
225
+ associated type ` Item ` from a trait ` Trait ` with the generics ` <'a> ` , the type
226
+ can be named like ` <Thing as Trait>::Item<'x> ` , where ` 'x ` is some lifetime in
227
+ scope. In this case, ` 'x ` will be used wherever ` 'a ` appears in the associated
228
+ type definitions on impls.
224
229
225
230
``` rust
226
231
trait AssociatedType {
@@ -249,6 +254,37 @@ fn main() {
249
254
}
250
255
```
251
256
257
+ An example of associated types with generics and where clauses:
258
+
259
+ ``` rust
260
+ struct ArrayLender <'a , T >(& 'a mut [T ; 16 ]);
261
+
262
+ trait Lend {
263
+ // Generic associated type declaration
264
+ type Lender <'a > where Self : 'a ;
265
+ fn lend <'a >(& 'a mut self ) -> Self :: Lender <'a >;
266
+ }
267
+
268
+ impl <T > Lend for [T ; 16 ] {
269
+ // Generic associated type definition
270
+ type Lender <'a > = ArrayLender <'a , T > where Self : 'a ;
271
+
272
+ fn lend <'a >(& 'a mut self ) -> Self :: Lender <'a > {
273
+ ArrayLender (self )
274
+ }
275
+ }
276
+
277
+ fn borrow <'a , T : Lend >(array : & 'a mut T ) -> <T as Lend >:: Lender <'a > {
278
+ array . lend ()
279
+ }
280
+
281
+
282
+ fn main () {
283
+ let mut array = [0usize ; 16 ];
284
+ let lender = borrow (& mut array );
285
+ }
286
+ ```
287
+
252
288
### Associated Types Container Example
253
289
254
290
Consider the following example of a ` Container ` trait. Notice that the type is
0 commit comments