Sub topic 11
Fn(&T) is sugar for a higher-ranked bound
This chapter assumes you know basic lifetime annotations, generics with trait bounds, and the Fn, FnMut and FnOnce traits. The previous chapter introduced for<'a>. Here you will see where it hides in everyday code.
The sugar
Lifetime elision lets you leave lifetimes out of function signatures. The same rules apply inside the parenthesized sugar of Fn, FnMut and FnOnce bounds. When you write a reference in the argument list and no lifetime, the compiler does not pick one lifetime for the whole function. It introduces a fresh lifetime that the caller of the closure chooses on each call.
These two bounds mean the same thing:
fn call_a<F>(f: F) -> usize
where
F: Fn(&str) -> usize,
{
f("hello")
}
fn call_b<F>(f: F) -> usize
where
F: for<'a> Fn(&'a str) -> usize,
{
f("hello")
}
fn main() {
println!("{}", call_a(|s| s.len()));
println!("{}", call_b(|s| s.len()));
}Both say that F accepts a &str of any lifetime. That is what lets apply_to_both in the previous chapter call f with a local borrow and a 'static literal.
Elision rules apply to the return type
The usual elision rules also connect the output to the input. With exactly one input lifetime, the output gets the same one:
| Written bound | Expanded bound |
|---|---|
| `Fn(&str) -> usize` | `for<'a> Fn(&'a str) -> usize` |
| `Fn(&str) -> &str` | `for<'a> Fn(&'a str) -> &'a str` |
| `Fn(&str, &str) -> usize` | `for<'a, 'b> Fn(&'a str, &'b str) -> usize` |
With two input references and a reference in the output, elision fails, just as it does for a plain function. You must then write the lifetimes yourself.
fn pick<F>(f: F) -> usize
where
F: for<'a> Fn(&'a str, &'a str) -> &'a str,
{
f("ab", "abc").len()
}
fn main() {
println!("{}", pick(|a, b| if a.len() >= b.len() { a } else { b }));
}Where the sugar does not reach
Elision only works inside the parenthesized Fn form. A reference in a type parameter position gets no automatic for<'a>:
fn call_c<'a, F>(f: F, s: &'a str) -> usize
where
F: Fn(&'a str) -> usize,
{
f(s)
}
fn main() {
println!("{}", call_c(|s| s.len(), "text"));
}Here 'a is fixed by the caller of call_c. F only has to accept that one lifetime. This is easier to satisfy, but call_c cannot call f with a borrow created inside its own body.
Choose between the forms by asking where the reference comes from:
- If the function receives the reference from its caller, a named lifetime like
'acan work. - If the function creates the reference itself, or calls
fwith several unrelated borrows, you need the higher-ranked form. TheFn(&T)sugar gives you that for free.
Why the default is the general one
Most closures taken as arguments are called with references the function does not control. A bound that only accepted one lifetime would fail as soon as the function called f twice with borrows of different lifetimes. Making the sugar higher-ranked removes that failure from the common case.
Exercise
Write the fully expanded form of the bound F: Fn(&str) -> &str. Then say why F: Fn(&str, &str) -> &str does not compile as written.
Show answer
The expansion is F: for<'a> Fn(&'a str) -> &'a str. With two input references, elision cannot tell which input lifetime the output borrows from, so the compiler rejects the signature. You must write explicit lifetimes, for example for<'a> Fn(&'a str, &'a str) -> &'a str.