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
What we want to show with `async fn` is that it automatically captures
all in-scope type and lifetime parameters in the returned opaque
`Future`. We're doing this to contrast it with RPIT in Rust 2021 and
earlier editions which does not capture in the returned opaque type
all in-scope lifetime parameters automatically.
However, our examples did not well demonstrate this, because the
examples used the lifetime parameters in the `async fn` return types,
which results in those lifetime parameters appearing in the associated
type of each returned opaque `Future`. In the RPIT desugarings, the
lifetime parameters would therefore appear in the bounds of the `impl
Trait` opaque types, and so would be captured regardless.
To better draw the distinction we want to draw, let's change each
`async fn` example to simply return the unit type (`()`). This
ensures that in the RPIT desugarings any lifetime parameters will not
be automatically captured under the rules of Rust 2021 and earlier
editions.
For each example, we'll use each of the type and lifetime parameters
in the body of the function so that they will appear in the returned
hidden type. This ensures that the RPIT desugarings for the examples
where a lifetime parameter is captured cannot be expressed without
using one of the tricks.
(Thanks to @tmandry for raising this important point.)
Copy file name to clipboardExpand all lines: text/3498-lifetime-capture-rules-2024.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,8 +24,8 @@ In return position `impl Trait` (RPIT) and `async fn`, an **opaque type** is a t
24
24
A hidden type is only allowed to name lifetime parameters when those lifetime parameters have been *"captured"* by the corresponding opaque type. For example:[^ref-captures-trait-ltps]
In the above, we would say that the lifetime parameter `'a` has been captured in the returned opaque type.
@@ -43,8 +43,8 @@ See [Appendix H] for examples and further exposition of these rules.
43
43
In return position `impl Trait` (RPIT) and `async fn`, lifetimes contained within all in-scope type parameters are captured in the opaque type. For example:[^ref-captures-trait-tps]
44
44
45
45
```rust
46
-
// Returns: Future<Output = T> + Captures<T>
47
-
asyncfnfoo<T>(x:T) ->T { x }
46
+
// Returns: Future<Output = ()> + Captures<T>
47
+
asyncfnfoo<T>(x:T) { _= (x,); }
48
48
49
49
fnbar<'a>(x:&'a ()) {
50
50
lety=foo(x);
@@ -68,8 +68,8 @@ The inconsistency is visible to users when desugaring from `async fn` to RPIT.
0 commit comments