On Rust 2024, why does a caller get E0597 when `above(&readings, &limit)` returns `impl Iterator<Item = &'a i32>` and `limit` is dropped first, and how does `use<..>` fix it?
answer
- what the opaque type captures
- every in-scope lifetime by default
- the threshold's anonymous lifetime
- precise capturing bound
basics
~20 sOn the 2024 edition, return-position impl Trait captures every in-scope lifetime, so the result counts as borrowing limit even if the body copied it. Writing + use<'a> (stable since 1.82) captures only the slice's lifetime.
solid answer
~40 sSince the 2024 edition (Rust 1.85), a return-position `impl Trait` without a `use<..>` bound captures all in-scope generic parameters, including the anonymous lifetime of `limit: &i32`. The caller must then keep `limit` alive as long as it uses the iterator, so dropping it first gives E0597, even though the body copied the value. Adding `+ use<'a>`, stable since 1.82, captures only `readings`' lifetime. Every type parameter must still be listed, so a function with argument-position `impl Trait` must name that parameter first. If the body really borrows `limit`, narrowing to `use<'a>` fails with E0700 instead. On 2021, free functions and inherent methods captured only lifetimes named in the bounds.
code
rust · 12 linesfn above<'a>(readings: &'a [i32], limit: &i32) -> impl Iterator<Item = &'a i32> + use<'a> {
let limit = *limit;
readings.iter().filter(move |r| **r > limit)
}
fn main() {
let readings = vec![12, 48, 7, 51];
let hot = {
let limit = 25;
above(&readings, &limit)
};
println!("{:?}", hot.collect::<Vec<_>>());
}go deeper
Recall that a returned impl Trait can keep its arguments borrowed, and that use<..> is the syntax for choosing which lifetimes it keeps.
Explain capture: on 2024 every in-scope lifetime is captured unless a use<..> bound narrows it, and captured borrows must outlive the returned value.
Diagnose an E0597 or E0502 caused by over-capture, fix it with use<..>, and recognise E0700 when the list is too narrow or argument-position impl Trait blocks it.
In public signatures, decide deliberately whether to capture a lifetime now: capturing reserves room to use it later, narrowing frees callers but becomes a promise.
## What "capturing" means Behind every return-position `impl Trait` is a hidden concrete type. The Rust Reference puts the rule this way: for the hidden type to **use** a generic parameter (a type, a const or a lifetime), the opaque type must **capture** it. Capturing has a second, visible effect: the caller treats the returned value as if it might borrow from every captured lifetime, so each captured borrow must stay alive while the value is used. ## The rule on Rust 2024 On the **2024 edition** (stable since Rust 1.85), a return-position `impl Trait` with no `use<..>` bound captures **every in-scope generic parameter, lifetimes included**, including the anonymous lifetimes of reference arguments. Type and const parameters were always captured. Before 2024, free functions and inherent methods captured a lifetime only if it appeared in the bounds; trait methods (return-position `impl Trait` in traits and their impls) and `async fn` already captured all lifetimes. The edition's migration is covered with the 2024 edition; here the point is the rule a current compiler applies. Take a filter over a borrowed slice with a threshold passed by reference: ```rust fn above<'a>(readings: &'a [i32], limit: &i32) -> impl Iterator<Item = &'a i32> { let limit = *limit; readings.iter().filter(move |r| **r > limit) } ``` The hidden type copies the threshold and borrows only `readings`. Yet on Rust 2024 the opaque type also captures the anonymous lifetime of `limit`, so a caller that drops the threshold early fails: ```rust let hot = { let limit = 25; above(&readings, &limit) // E0597: `limit` does not live long enough }; ``` rustc 1.99.1 adds the note "this call may capture more lifetimes than intended, because Rust 2024 has adjusted the `impl Trait` lifetime capture rules" and suggests a `use<..>` bound. The same code compiles with `--edition 2021`. ## Precise capturing with `use<..>` Since **Rust 1.82**, a `use<..>` bound lists exactly what the opaque type captures. `-> impl Iterator<Item = &'a i32> + use<'a>` captures `'a` only, so the caller may drop `limit` after the call. Its constraints, as rustc 1.99 enforces them: - **Every in-scope type and const parameter must be listed**; `fn wrap<T>(x: T) -> impl Sized + use<>` fails with "`impl Trait` must mention all type parameters in scope in `use<...>`". - A function with **argument-position `impl Trait`** cannot use `use<..>`, because its anonymous type parameter cannot be named; give the parameter a name first. - Lifetimes go before type parameters, and any lifetime used in the other bounds (here `'a` in `Item = &'a i32`) must be listed. - Under-capturing is caught: if the body really does use `limit`'s borrow and the bound says `use<'a>`, rustc reports **E0700**, "hidden type … captures lifetime that does not appear in bounds". - In **trait method** signatures, `use<..>` is stable since **Rust 1.87** and must include `Self`. ## Before `use<..>` Older code shows two workarounds you should recognise and may replace: 1. the **outlives trick**, `-> impl Trait + 'a`, which needed extra bounds such as `T: 'a` and over-restricted callers; 2. the **`Captures` trick**, a helper trait bound such as `impl Sized + Captures<&'a ()>` that forced a lifetime into the bounds. On 2024, both are usually unnecessary: either the default captures what you need, or `use<..>` narrows it. ## Reading a capture error Over-capture shows up at the **call site**, not at the definition, which is what makes it confusing in review: - the error is an ordinary borrow error (**E0597** for a value dropped too early, **E0502** when the caller later mutates something it lent to the call); - the span points at a borrow whose value the hidden type does not actually hold; - rustc's note about the 2024 capture rules, plus a `use<...>` help line, is the tell. The fix belongs in the **callee's signature**: add `use<..>` listing only the lifetimes and type parameters the hidden type really uses, then let the compiler confirm the list with E0700 if it is too short. ## Choosing what to capture | Signature tail | Captures | Caller may drop `limit` early? | |---|---|---| | `impl Iterator<Item = &'a i32>` (2024) | `'a` and `limit`'s lifetime | no, E0597 | | `impl Iterator<Item = &'a i32> + use<'a>` | `'a` only | yes | | `use<'a>` while the body borrows `limit` | — | does not compile, E0700 | Leaving the default in place is a deliberate choice too: capturing a lifetime you do not use yet reserves the right to use it later without a breaking change, at the cost of stricter callers today.
- In Rust, what does rustc report if you write `use<'a>` but the hidden type still borrows `limit`?E0700: "hidden type for `impl Iterator<Item = &'a i32>` captures lifetime that does not appear in bounds", with a help line suggesting `use<'a, '_>`. The `use<..>` list is checked against what the body actually returns, so precise capturing cannot hide a real borrow; it only removes borrows the hidden type does not have.
- In Rust, why can `use<'a>` not be added when the function also takes `keep: impl Fn(&i32) -> bool`?A `use<..>` bound must list every in-scope type parameter, and argument-position `impl Trait` creates an anonymous one that cannot be named. rustc 1.99 rejects it with "`impl Trait` must mention all type parameters in scope in `use<...>`". Turn it into a named parameter, `<F: Fn(&i32) -> bool>`, and write `use<'a, F>`; note that callers can now turbofish `F`.
- impl Trait only captures the lifetimes that appear in its bounds on 2024
- use<'a> makes the returned value live longer than 'a
- use<> can leave out a generic type parameter that is in scope
- Copying *limit inside the body frees the caller from keeping limit alive
- use<..> bounds are a nightly-only feature
Assumes Rust 1.99 on the 2024 edition (stable since 1.85), which captures all in-scope lifetimes; use<..> is stable since 1.82, and in trait method signatures since 1.87.