Skip to content

Commit

Permalink
Remove the explainer about use<..>
Browse files Browse the repository at this point in the history
  • Loading branch information
traviscross committed Aug 6, 2024
1 parent a831726 commit b1cda1e
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions src/rust-2024/rpit-lifetime-capture.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

🚧 The 2024 Edition has not yet been released and hence this section is still "under construction".

This chapter describes changes related to the **Lifetime Capture Rules 2024** introduced in [RFC 3498], including opaque type *precise capturing* introduced in [RFC 3617].
This chapter describes changes related to the **Lifetime Capture Rules 2024** introduced in [RFC 3498], including how to use opaque type *precise capturing* (introduced in [RFC 3617]) to migrate your code.

[RFC 3498]: https://github.com/rust-lang/rfcs/pull/3498
[RFC 3617]: https://github.com/rust-lang/rfcs/pull/3617

## Summary

- In Rust 2024, *all* in-scope generic parameters, including lifetime parameters, are implicitly captured when the `use<..>` bound is not present.
- In all editions, `use<..>` bounds allow for specifying explicitly which lifetime parameters to capture in RPIT opaque types.
- Uses of the `Captures` trick (`Captures<..>` bounds) and of the outlives trick (e.g. `'_` bounds) can be replaced by `use<..>` bounds (in all editions) or removed entirely (in Rust 2024).

## Details

### Capturing and `use<..>` syntax
### Capturing

*Capturing* a generic parameter in an RPIT (return-position impl Trait) opaque type allows for that parameter to be used in the corresponding hidden type. In Rust 1.82, we added `use<..>` bounds that allow specifying explicitly which generic parameters to capture. E.g.:
*Capturing* a generic parameter in an RPIT (return-position impl Trait) opaque type allows for that parameter to be used in the corresponding hidden type. In Rust 1.82, we added `use<..>` bounds that allow specifying explicitly which generic parameters to capture. Those will be helpful for migrating your code to Rust 2024, and will be helpful in this chapter for explaining how the edition-specific implicit capturing rules work. These `use<..>` bounds look like this:

```rust
# #![feature(precise_capturing)]
Expand All @@ -34,18 +33,7 @@ fn capture<'a, T>(x: &'a (), y: T) -> impl Sized + use<'a, T> {
}
```

Currently, `use<..>` bounds must list all generic type and const parameters in scope, but they may exclude in-scope lifetime parameters. E.g.:

```rust
# #![feature(precise_capturing)]
fn capture<'a, T>(_: &'a (), _: T) -> impl Sized + use<T> {
// ~~~~~~~~~~~~~~~~~~~
// The opaque type captures only `T` and not `'a`.
()
}
```

You might want to capture more lifetimes in order to return a hidden type that uses those lifetimes, as we saw in example at the top of this section. Conversely, you might want to capture fewer lifetimes in order to allow the opaque type to be used in more places. E.g., this is an error because the lifetime is captured despite the fact that the hidden type does not use the lifetime:
The generic parameters that are captured affect how the opaque type can be used. E.g., this is an error because the lifetime is captured despite the fact that the hidden type does not use the lifetime:

```rust,compile_fail
# #![feature(precise_capturing)]
Expand All @@ -57,7 +45,7 @@ fn test<'a>(x: &'a ()) -> impl Sized + 'static {
}
```

Conversely, this is OK since the opaque type does not capture the lifetime:
Conversely, this is OK:

```rust
# #![feature(precise_capturing)]
Expand All @@ -68,8 +56,6 @@ fn test<'a>(x: &'a ()) -> impl Sized + 'static {
}
```

The bounds list of an RPIT opaque type may contain at most one `use<..>` bound. If this bound is present, then the listed generic parameters are the only ones captured.

### Edition-specific rules when no `use<..>` bound is present

If the `use<..>` bound is not present, then the compiler uses edition-specific rules to decide which in-scope generic parameters to capture implicitly.
Expand Down

0 comments on commit b1cda1e

Please sign in to comment.