Lifetimes
A lifetime annotation does not change when anything is dropped. It is a name for a relationship the compiler already tracks, written down so it can check that the relationship holds.
The chapter is ordered so the first article explains why the annotation exists at all, and the last one explains how to write code that never needs one.
Articles
Section titled “Articles”Nothing published in this chapter yet — all 15 articles are still to come.
Coming up (15)
- What are lifetimes in Rust?a name for something the compiler already tracks
- Why does Rust need lifetime annotations?the two-argument function with one answer
- How to read a lifetime annotation’a describes a relationship, not a duration
- What is lifetime elision?the three cases the compiler handles for you
- What is a lifetime bound?’a: ’b, and who must outlive whom
- How to use ’staticliterals, leaks, and the overuse of it
- What does ’static mean for a string?string literals live in the binary
- How lifetimes work with structsa struct cannot outlive what it borrows
- What is variance in lifetimes?covariance, and the cases that surprise people
- How to fix a lifetime error in a methodelision picks &self, and that is usually right
- How to return a reference from a functionyou need one of the inputs, or an owned value
- How lifetimes work with trait objectsBox<dyn Trait + ’a> decoded
- Do lifetimes exist at runtime?they are erased, and what that means
- How to avoid lifetime annotations entirelyown the data, or take it by value
- What is a dangling reference, and how does Rust prevent it?lifetimes as a compile-time guarantee