Why lifetimes
What you'll learn:
- explain that a reference's lifetime is a stretch of code, not seconds, and why a reference cannot outlive its value;
- read the signature
fn longest<'a>(x: &'a str, y: &'a str) -> &'a strand predict error E0597; - tell when the compiler fills in a lifetime by itself and when it asks you to write one (E0106);
- store a reference in a struct and explain what
'staticmeans on a string literal.

Picture a library reading room. An owner has left a book there until Friday, and on Friday will take it home. Everyone who wants to read it gets a ticket, and the librarian stamps it with a date: the last day the ticket lets you take the book.
The librarian's main rule: a ticket cannot be valid longer than the book stays in the room. A ticket "until Saturday" for a book that leaves on Friday would cheat the reader.
In Rust, the book is a value, the owner is its owner, and the ticket is a reference. In the borrowing theme you saw that the owner may not throw the book away while it is lent. Now let's look at the same story from the ticket's side: how long does a reference stay valid?
Take a program that creates a reference in an inner block and reads it outside:
fn main() {
let r;
{
let s = String::from("Dune");
r = &s; // ✗
}
println!("{r}");
}error[E0597]: `s` does not live long enoughLet's go through it line by line:
let r;declares a variable for the reference, with no value yet.- In the inner block,
sgets the stringDune, andrgets a reference to it. - At the block's
}, the ownersgoes away, and the string is freed. println!readsrafter that: the reference would point to a freed string.
The compiler answers that s does not live long enough. The ticket was issued until Saturday, but the book left on Friday.
Time is a stretch of code
The word "time" here is not about seconds. A reference's lifetime is the stretch of code where it is used: from the place the reference is created to the last place it is read. A value has its own stretch too: from its creation to the } of its owner's block, or to a move.
The borrow checker compares these two stretches and demands one thing: the reference's stretch must lie entirely inside the value's stretch. In the example above, the stretch of r runs to println!, while the string's stretch ends earlier, at the block's }. The reference's stretch sticks out, and that is error E0597.
Let's move the print inside the block:
fn main() {
let r;
{
let s = String::from("Dune");
r = &s;
println!("{r}"); // Dune
}
}Now the program builds. The name r is visible to the end of main, but the reference's lifetime ends at println!, its last use. The string lives until the block's }, which is longer than the reference, so the rule holds.
The compiler only checks
Lifetimes exist only at build time. The finished program has none: they change nothing in how it runs and do not slow it down. The compiler checks every reference once, and if even one could outlive its value, the program does not build.
Inside a function, the compiler finds the stretches of references by itself, so nobody writes them in a let. Sometimes you have to name them explicitly: in a function signature or in a struct definition.
Why so strict? In C and C++ the code from the first example builds, and reading through a dangling reference is undefined behavior: the program may print garbage or crash. Rust catches the same error at build time, and the running program spends not a single cycle on the check.
This is where the library picture stops being accurate. The stamp on a ticket is a calendar date, and the librarian checks it every day. In Rust, the "date" is a place in the code, and the compiler checks it once, before the program runs.
Interviewers like to ask this: "What is a lifetime in Rust?" The answer:
- a reference's lifetime is the stretch of code where the reference is used, not a time in seconds;
- the borrow checker demands that the value be alive on that whole stretch;
- if a reference could outlive its value, the compiler stops with an error such as E0597;
- lifetimes are checked only at build time and have no effect on how the program runs.
Common mistake
A loop creates strings and puts references to them into a vector:
fn main() {
let mut names: Vec<&str> = Vec::new();
for i in 1..=3 {
let name = format!("user{i}");
names.push(&name); // ✗
}
println!("{names:?}");
}error[E0597]: `name` does not live long enoughThe string name is created anew on each pass of the loop and freed at its }. But the vector names keeps the references and prints them after the loop: the references would outlive their strings. The fix is to store the strings themselves in the vector: Vec<String> and names.push(name). Then each string moves into the vector and lives as long as the vector does.
Key points
- A reference's lifetime is the stretch of code from its creation to its last use, not seconds.
- The value must be alive on the reference's whole stretch, or the compiler answers with error E0597.
- Lifetimes are checked only at build time and change nothing at run time.
- References to strings created in a loop cannot be kept after the loop: store the
Strings themselves.