Sub topic 9
Why a fixed lifetime parameter is not enough
This chapter assumes you can read basic lifetime annotations, write generic functions with trait bounds, and use the Fn traits. It also assumes you know that a longer lifetime can stand in for a shorter one, from the earlier chapters on subtyping.
Who picks the lifetime?
A lifetime parameter on a function, such as 'a in fn f<'a>, is chosen by the caller. Inside the body, 'a is one fixed lifetime that is longer than the whole call. The body cannot create a borrow that lasts that long. Any local variable is dropped when the function returns.
This matters when a function receives a closure and calls it with references it creates itself.
A bound that fails
Here the function tries to name the lifetime with a parameter on the function:
// Does not compile.
fn apply_to_both<'a, F>(f: F) -> (usize, usize)
where
F: Fn(&'a str) -> &'a str,
{
let owned = String::from(" owned text ");
let a = f(&owned).len(); // error: `owned` does not live long enough
let b = f(" literal ").len();
(a, b)
}The bound F: Fn(&'a str) -> &'a str says that f accepts one particular lifetime 'a, and the caller picks it. Calling f(&owned) needs 'a to be no longer than the borrow of owned. But 'a outlives the function body, and owned does not. The compiler rejects the call.
The problem is the quantifier. The bound says "there is one 'a that f accepts". The function needs "f accepts every 'a".
Quantifying over all lifetimes
A higher-ranked trait bound (HRTB) states the second meaning. The syntax for<'a> means "for every lifetime 'a":
fn apply_to_both<F>(f: F) -> (usize, usize)
where
F: for<'a> Fn(&'a str) -> &'a str,
{
let owned = String::from(" owned text ");
let a = f(&owned).len();
let b = f(" literal ").len();
(a, b)
}
fn main() {
let r = apply_to_both(|s| s.trim());
println!("{r:?}");
}'a is no longer a parameter of apply_to_both. Each call to f picks its own 'a. The first call uses a short borrow of owned, and the second uses 'static. Every call is valid because f promises to work for any lifetime.
| Form | Who chooses `'a` | Can `f` take a local borrow? |
|---|---|---|
| `fn apply<'a, F: Fn(&'a str) -> &'a str>` | The caller, once | No |
| `F: for<'a> Fn(&'a str) -> &'a str` | Each call to `f` | Yes |
The cost falls on the caller. A closure that must work for every lifetime is a stricter requirement than one that works for a single lifetime. A closure that returns a reference tied to captured data, for example, will not satisfy it.
Takeaways
- A lifetime parameter on a function is fixed for the whole call and outlives the body.
- If the function creates a borrow itself and passes it to a callback, the callback's bound must hold for every lifetime.
for<'a>expresses that. The next chapter covers how to read and write it.
Exercise
In the failing apply_to_both<'a, F> above, which line causes the error and why? Explain it in terms of who chooses 'a.
Show answer
The line let a = f(&owned).len(); fails. The caller chooses 'a, so it must outlive the function body. The call needs 'a to fit inside the borrow of the local owned, which is dropped when the function returns. No single 'a satisfies both. Replacing the bound with F: for<'a> Fn(&'a str) -> &'a str lets each call to f pick its own lifetime.