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
We use the `Captures` trait for exposition before formally introducing
it in a later section. In an earlier commit, we had added some text
in parentheses to explain this.
Rather than breaking up the flow with parenthesized text, let's use
footnotes for this.
In the second place that we do this, we're using `Captures` only for
exposition; real code would not need to use the trick here. We'll
mention that in the footnote as well.
Copy file name to clipboardExpand all lines: text/3498-lifetime-capture-rules-2024.md
+6-4Lines changed: 6 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,15 +21,13 @@ This RFC specifies a [solution] that achieves this. But first, we'll describe t
21
21
22
22
In return position `impl Trait` (RPIT) and `async fn`, an **opaque type** is a type that can only be used for its specified trait bounds (and for the "leaked" auto trait bounds of its hidden type). A **hidden type** is the actual concrete type of the values hidden behind the opaque type.
23
23
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:
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]
(See [below](#the-captures-trick) for the definition of `Captures`.)
32
-
33
31
In the above, we would say that the lifetime parameter `'a` has been captured in the returned opaque type.
34
32
35
33
For an opaque type that *does not* specify an outlives bound (e.g. `+ 'other`), when a caller receives a value of that opaque type and wants to prove that it outlives some lifetime, the caller must prove that all of the captured lifetime components of the opaque type outlive that lifetime. The captured lifetime components are the set of lifetimes contained within captured type parameters and the lifetimes represented by captured lifetime parameters.
@@ -38,9 +36,11 @@ For an opaque type that *does* specify an outlives bound (e.g. `+ 'other`), when
38
36
39
37
See [Appendix H] for examples and further exposition of these rules.
40
38
39
+
[^ref-captures-trait-ltps]: See ["The `Captures` trick"](#the-captures-trick) for the definition of `Captures`.
40
+
41
41
## Capturing lifetimes in type parameters
42
42
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:
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
46
// Returns: Future<Output = T> + Captures<T>
@@ -55,6 +55,8 @@ fn bar<'a>(x: &'a ()) {
55
55
56
56
In the above, we would say that `foo` captures the type parameter `T` or that it "captures all lifetime components contained in the type parameter `T`". Consequently, the call to `foo` captures the lifetime `'a` in its returned opaque type.
57
57
58
+
[^ref-captures-trait-tps]: See ["The `Captures` trick"](#the-captures-trick) for the definition of `Captures`. Note that in this example, the `Captures` trick would not be needed, but it is notated explicitly for exposition.
59
+
58
60
### Behavior of `async fn`
59
61
60
62
As we saw in the examples above, `async` functions automatically capture in their returned opaque types all type and lifetime parameters in scope.
0 commit comments