Lifetimes in Rust
by Hexagon, 3 min read rust guide
Sooner or later, you'll see something like <'a> in Rust code, and wonder what it means. Lifetimes are how Rust makes sure that references never point to data that's gone. Most of the time they're invisible, but when they show up, it helps to know what they say.
The Problem
In the article about ownership, we learned that a reference must never outlive the value it points to. Here's a program that breaks that rule:
value is freed at the end of the inner block, so reference would point to
freed memory. The compiler tracks how long every value lives, called its
lifetime, and refuses the program.
Within a single function, the compiler can see everything, and figures this out by itself. Lifetimes only need to be written out when a function or a struct works with references, and the compiler can't tell how they're connected.
Lifetimes in Functions
Here's a function that returns the longer of two strings:
The problem is that the returned reference points into either a or b, and
the compiler doesn't know which. So it can't check that the caller uses the
result safely. We have to tell it:
'a is a lifetime parameter, a name for "some lifetime". The names are short by
convention, 'a, 'b and so on. The signature now says: both inputs live at
least as long as 'a, and the result lives as long as 'a too. In practice,
the result can be used as long as both inputs are alive.
Note that lifetimes don't change how long anything lives. They're labels that describe how references relate, so the compiler can check them.
If we move the println! outside the inner block, where second is gone, the
compiler stops us, as the result might point into second.
Lifetime Elision
You've written plenty of functions with references in this series, without any
'a. That's because the compiler fills in lifetimes for common cases, called
lifetime elision:
- Each reference parameter gets its own lifetime.
- If there's exactly one reference parameter, the result gets its lifetime.
- If one of the parameters is
&selfor&mut self, the result gets the lifetime ofself.
So this function needs no annotations, as rule 2 applies:
longest needed them because it has two reference parameters, and no self,
so none of the rules decide the lifetime of the result.
Lifetimes in Structs
A struct that holds a reference must say how long that reference lives:
This means that an Excerpt can't outlive the string it points into. If you
drop novel while excerpt is still in use, the program won't compile.
A tip for beginners: when a struct with references gets in your way, make it
own its data instead, with String instead of &str. That removes the lifetime
completely. References in structs are great for performance, but they're rarely
needed when you start out.
The Static Lifetime
'static is a special lifetime, that lasts for the whole program. All string
literals have it, as they're built into the program itself:
You'll also see 'static in error messages, often when sending data to
threads. If the compiler asks for 'static, it usually means "this must not
borrow anything from the current function". The fix is often to move or clone
the data, rather than adding 'static everywhere.
Conclusion
Lifetimes look scary, but they don't add anything new. They're a way to write down the borrowing rules you already know, in places where the compiler can't figure them out by itself. When you get a lifetime error:
- Look at which reference outlives what. The error message points to both.
- Ask if the function or struct could own the data instead.
- If you need the reference, add lifetime parameters that describe how the output relates to the inputs.
Most Rust code has very few lifetime annotations, so don't worry if you don't need them for a while.