Skip to content

Commit 1691e6e

Browse files
committed
Add examples containing generic parameters
1 parent 2b921f9 commit 1691e6e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

text/0000-impl-trait-type-aliases.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,26 @@ type Foo = impl Bar;
298298

299299
In addition, when documenting `impl Trait`, explanations of the feature would avoid type theoretic terminology (specifically "existential types") and prefer type inference language (if any technical description is needed at all).
300300

301+
`impl Trait` type aliases may contain generic parameters just like any other type alias. The type alias must contain the same type parameters as its concrete type, except those implicitly captured in the scope (see [RFC 2071](https://github.com/rust-lang/rfcs/blob/master/text/2071-impl-trait-existential-types.md) for details).
302+
303+
```rust
304+
// `impl Trait` type aliases may contain type parameters...
305+
#[derive(Debug)]
306+
struct DebugWrapper<T: Debug>(T);
307+
308+
type Foo<T> = impl Debug;
309+
310+
fn get_foo<T: Debug>(x: T) -> Foo<T> { DebugWrapper(x) }
311+
312+
// ...and lifetime parameters (and so on).
313+
#[derive(Debug)]
314+
struct UnitRefWrapper<'a>(&'a ());
315+
316+
type Bar<'a> = impl Debug;
317+
318+
fn get_bar<'a>(y: &'a ()) -> Bar<'a> { UnitRefWrapper(y) }
319+
```
320+
301321
## Restricting compound `impl Trait` trait aliases
302322
The type alias syntax is more flexible than `existential type`, but for now we restrict the form to that equivalent to `existential type`. That means that, if `impl Trait` appears on the right-hand side of a type alias declaration, it must be the only type. The following compound type aliases, therefore, are initially forbidden:
303323

0 commit comments

Comments
 (0)