Sub topic 2
Lifetime subtyping: when a longer lifetime can stand in for a shorter one
This chapter assumes you know Rust references, basic lifetime annotations on functions and structs, and generic trait bounds. It covers the rule that lets the compiler accept a long-lived reference where a short-lived one is expected.
Outlives as a subtype relation
A lifetime names a region of code in which a reference is valid. If 'long covers the whole of 'short and more, we write 'long: 'short and say 'long outlives 'short.
Rust treats this as subtyping. Because 'long: 'short, &'long T is a subtype of &'short T. A subtype can be used wherever its supertype is expected. A longer-lived reference is therefore always a valid stand-in for a shorter-lived one. The reverse is never allowed.
The special lifetime 'static outlives every other lifetime. A &'static str can be used where any &'a str is expected.
Subtyping in action
You rarely write this conversion yourself. The compiler inserts it at each use site. Look at longest, which takes two references that share one lifetime 'a.
fn longest<'a>(a: &'a str, b: &'a str) -> &'a str {
if a.len() >= b.len() { a } else { b }
}
fn main() {
let outer = String::from("lives longer");
{
let inner = String::from("short");
let r = longest(&outer, &inner);
println!("{r}");
}
}The two arguments have different lifetimes. &outer lives to the end of main, while &inner lives only to the end of the inner block. The signature demands one 'a for both. The compiler picks 'a as the inner block, the shorter region. It shrinks the borrow of outer to that region by subtyping. The result r is valid only inside the block, and it is used only there, so the program compiles.
This is why the signature of longest is not too strict in practice. 'a is not "the lifetime of a". It is a region that both arguments can be shrunk to.
Declaring outlives bounds
You can state the relation yourself with a bound on a lifetime parameter. It is the same 'long: 'short notation.
fn pick_first<'long: 'short, 'short>(x: &'long str, _y: &'short str) -> &'short str {
x
}
fn main() {
let a = String::from("first");
let b = String::from("second");
println!("{}", pick_first(&a, &b));
}Returning x as &'short str is legal only because 'long: 'short makes &'long str a subtype of &'short str. Remove the bound and the compiler reports that lifetime 'short is required but 'long is not known to outlive it.
What to remember
- Subtyping on lifetimes runs from longer to shorter.
- The compiler applies it implicitly when it unifies several references into one lifetime parameter.
- Whether a type can be shrunk this way depends on how it uses the lifetime.
&'a Tcan shrink. Some types, such as&mut T, cannot. The next chapters call this property variance.
Exercise
In fn f<'a>(x: &'a str, y: &'a str) -> &'a str, a caller passes &s1 where s1 lives for the whole function, and &s2 where s2 lives only in an inner block. Which region does the compiler choose for 'a, and what rule lets &s1 be accepted?
Show answer
It chooses the inner block, the shorter region. Because 'long: 'short, &'long str is a subtype of &'short str, so the borrow of s1 is shrunk to that region.