Lifetimes
A lifetime is the region of code where a reference must stay valid. Every reference in Rust has one, and the borrow checker makes sure that the value the reference points to is alive across that whole region. The rule "a reference does not outlive its value" is familiar from the Borrowing topic: there the compiler could see on its own where a value dies and where a reference is used. Inside a single function it stays that way — lifetimes are inferred, and you do not write them.
The difficulty starts at a function boundary. The compiler checks each function separately, and about a called function it knows only its signature. If a function takes two references and returns a reference, the signature has to show which argument the result is borrowed from — that is what annotations like 'a are for. They extend nothing and have no effect on how the program runs: they describe a relationship between references, and the compiler checks that the function body and every call respect it. In simple signatures the elision rules fill lifetimes in, so annotations are rare in code: in functions with several references, in structs that hold references, and in the 'static bound. This topic walks lifetimes layer by layer — from why they exist to 'static.
Topic map
- Why Lifetimes — a reference's lifetime lasts from its creation to its last use; the borrow checker compares it with the value's life and reports E0597
does not live long enough; annotations describe relationships and extend nothing. - Lifetime Annotations —
<'a>and&'a str; the result oflongest<'a>lives no longer than the shorter argument; a call that does not compile even though it would return a live string; the'b: 'abound; for seniors — variance andfor<'a>. - Elision Rules — the three rules by which the compiler fills lifetimes into a signature itself; why
fn first(s: &str) -> &strneeds no annotations while two references in and one out give E0106; methods with&selfand rule 3. - Structs Holding References —
struct Excerpt<'a> { part: &'a str }promises that a value will not outlive the data it points to;impl<'a> Excerpt<'a>and'_; a-> &'a strgetter versus-> &str; why a struct cannot point into its own field. - The
'staticLifetime —&'static strfor literals andstaticitems; theT: 'staticbound, which an ownedStringsatisfies too;thread::spawnandBox<dyn Trait>;Box::leak;'staticdoes not mean "lives in the executable".
Common mistakes and traps
| Mistake | Consequence |
|---|---|
| Adding annotations to "extend" a value's life | An annotation extends nothing: the value is freed at the end of its owner's scope, and the compiler reports E0597 or E0515 |
| Returning a reference from a function with two reference parameters without an annotation | error[E0106]: missing lifetime specifier — the signature does not show whether the result is borrowed from x or y |
Expecting longest(&a, &b) to compile because the longer string will be returned | The compiler checks the signature, not the values: the result lives no longer than the shorter argument — error[E0597]: `b` does not live long enough |
Giving every reference the same 'a when the result is borrowed from only one argument | An extra demand on the caller; fn first<'a, 'b>(x: &'a str, y: &'b str) -> &'a str accepts more programs |
Assuming T: 'static requires data that lives for the whole program | String, Vec<i32> and any type without borrowed references satisfy the bound, yet they are freed as usual |
Storing a &str in a struct that has to live on its own | The struct is tied to someone else's data and cannot outlive it; a String field makes the question go away |
Why it matters in interviews
Lifetimes are the line between junior and middle in Rust: the 'a syntax is easy to learn, but explaining what it means is not. A strong answer rests on one idea: an annotation is the signature's contract about where the result is borrowed from, not an order to extend a value's life. At the senior level variance and higher-ranked bounds for<'a> are added on top.
What interviewers usually check:
- What a lifetime is, and why annotations are needed if the compiler already sees where values die.
- Fixing
fn longest(x: &str, y: &str) -> &str, explaining how long the result lives, and why a call with a short-lived argument does not compile. - The three elision rules and why
fn first(s: &str) -> &strneeds no annotation. - Why a struct holding a reference needs a lifetime parameter and what it promises.
- How
&'static strdiffers from theT: 'staticbound, and whythread::spawnrequires it. - For seniors: why
&mut Tis invariant inTand whatfor<'a>means.