Skip to content

Commit 60cd80a

Browse files
committed
Add basic GATs info
1 parent 9ea36a3 commit 60cd80a

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/items/associated-items.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,12 @@ If a type `Item` has an associated type `Assoc` from a trait `Trait`, then
220220
associated type definition. Furthermore, if `Item` is a type parameter, then
221221
`Item::Assoc` can be used in type parameters.
222222

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.
224229

225230
```rust
226231
trait AssociatedType {
@@ -249,6 +254,37 @@ fn main() {
249254
}
250255
```
251256

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+
252288
### Associated Types Container Example
253289

254290
Consider the following example of a `Container` trait. Notice that the type is

src/items/type-aliases.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
> _TypeAlias_ :\
55
> &nbsp;&nbsp; `type` [IDENTIFIER]&nbsp;[_GenericParams_]<sup>?</sup>
66
> ( `:` [_TypeParamBounds_] )<sup>?</sup>
7-
> [_WhereClause_]<sup>?</sup> ( `=` [_Type_] )<sup>?</sup> `;`
7+
> [_WhereClause_]<sup>?</sup> ( `=` [_Type_] )<sup>?</sup> [_WhereClause_]<sup>?</sup> `;`
88
99
A _type alias_ defines a new name for an existing [type]. Type aliases are
1010
declared with the keyword `type`. Every value has a single, specific type, but
@@ -37,6 +37,9 @@ A type alias without the [_Type_] specification may only appear as an
3737
A type alias with [_TypeParamBounds_] may only specified when used as
3838
an [associated type] in a [trait].
3939

40+
A type alias with where clauses after the equals sign may only appear as an
41+
[associated type] in a [trait] or a [trait impl].
42+
4043
[IDENTIFIER]: ../identifiers.md
4144
[_GenericParams_]: generics.md
4245
[_TypeParamBounds_]: ../trait-bounds.md
@@ -45,3 +48,4 @@ an [associated type] in a [trait].
4548
[associated type]: associated-items.md#associated-types
4649
[trait]: traits.md
4750
[type]: ../types.md
51+
[trait impl]: implementations.md#trait-implementations

0 commit comments

Comments
 (0)