Step Into Rust · Lesson 8 · Robust Rust
Lifetimes
How long a borrow lives: lifetime parameters, structs that borrow, and zero-copy parsing.
- ~40 minreading and coding
- 4missions
- membersaccess
- Rustlanguage
Part of Step Into Rust. The full lesson, its missions and the real compiler and debugger are in the course. Try a free lesson first to see how it works.
A lifetime is the stretch of code where a reference is valid. The borrow checker makes sure no reference outlives the data it points to: the dangling pointers that cause use-after-free bugs in C simply don't compile.
In this lesson
- Every reference has a lifetime
- Usually you don't write them
- When the compiler can't tell
- Structs that borrow
- 'static
- Returning references to locals
Missions
- longest
- A struct that borrows
- Make it an Iterator
- Zero-copy binary parsing