You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Reference didn't include any description of capturing or capturing
behavior for `impl Trait` opaque types. Let's describe briefly what
capturing is and what the currently-stable automatic capturing rules
are. Then let's describe the syntax and behavior of RFC 3617 precise
capturing.
[Trait] and lifetime bounds provide a way for [generic items][generic] to
24
38
restrict which types and lifetimes are used as their parameters. Bounds can be
@@ -227,20 +241,26 @@ trait Trait<'a, T: 'a> {}
227
241
impl<'a, T> Trait<'a, T> for&'aT {}
228
242
```
229
243
244
+
## Use bounds
245
+
246
+
Certain bounds lists may include a `use<..>` bound to control which generic parameters are captured by the `impl Trait`[abstract return type]. See [precise capturing] for more details.
Copy file name to clipboardExpand all lines: src/types/impl-trait.md
+30-3
Original file line number
Diff line number
Diff line change
@@ -91,7 +91,33 @@ Functions in traits may also use `impl Trait` as a syntax for an anonymous assoc
91
91
92
92
Every `impl Trait` in the return type of an associated function in a trait is desugared to an anonymous associated type. The return type that appears in the implementation's function signature is used to determine the value of the associated type.
93
93
94
-
### Differences between generics and `impl Trait` in return position
94
+
## Capturing
95
+
96
+
Behind each return-position `impl Trait` abstract return type is some hidden concrete type. For this concrete type to use a generic parameter, that generic parameter must be *captured* by the abstract type.
97
+
98
+
## Automatic capturing
99
+
100
+
Return-position `impl Trait` abstract return types automatically capture certain of the in-scope generic parameters. Everywhere, these automatically capture all in-scope type and const generic parameters.
101
+
102
+
On items of trait impls and trait definitions, these types additionally automatically capture all in-scope generic lifetime parameters, including higher-ranked ones. On free functions and on associated functions and methods of inherent impls, only the generic lifetime parameters that appear in the bounds of abstract return type are captured.
103
+
104
+
## Precise capturing
105
+
106
+
The set of generic parameters captured by a return-position `impl Trait` abstract type may be explicitly controlled with a [`use<..>` bound]. If present, only the generic parameters listed in the bound will be captured. E.g.:
Currently, only one `use<..>` bound may be present in a bounds list, such bounds are not allowed in the signature of items of a trait definition, and all in-scope type and const generic parameters must be included. If the elided lifetime (`'_`) is otherwise allowed to appear within the `impl Trait` return type, it may be named within the `use<..>` bound.
117
+
118
+
Because all in-scope type parameters must be included by name, a `use<..>` bound may not be used in the signature of items that use argument-position `impl Trait`, as those items have anonymous type parameters in scope.
119
+
120
+
## Differences between generics and `impl Trait` in return position
95
121
96
122
In argument position, `impl Trait` is very similar in semantics to a generic type parameter.
97
123
However, there are significant differences between the two in return position.
@@ -127,9 +153,10 @@ Instead, the function chooses the return type, but only promises that it will im
127
153
`impl Trait` can only appear as a parameter or return type of a non-`extern` function.
128
154
It cannot be the type of a `let` binding, field type, or appear inside a type alias.
0 commit comments