
Understand it
An annotation does not change how long anything lives. It tells the compiler how input and output borrows are connected, so it can prove a returned reference never outlives the data behind it. Most of the time inference handles this and you write nothing at all.
A lifetime is a relationship the compiler uses to prove a reference is valid when it is used. It is not a timer and an annotation cannot keep memory alive. In longest, both input references are related to the returned reference through 'a. The caller must use the result only while the selected input could still be valid. This is conservative: at compile time the function could return either argument, so the result is limited by the shorter applicable borrow.
Trace a call where one string lives for the whole function and another is created in an inner block. You may compare them and print the chosen slice inside that block. You cannot keep the returned reference for use after the inner string has gone away, because it might point into that string. Moving a lifetime annotation to a different letter would not change the data's scope. The remedy is to shorten the use of the result, return an owned String, or change which data the result can borrow from.
Most functions do not need written lifetime parameters. A function with one borrowed input and a borrowed output often has an obvious relationship, and Rust applies elision rules. Add annotations when multiple inputs make the source of the output ambiguous or when a struct stores a reference. In an error message, identify the owner of each referenced value and the last point at which it is alive. That timeline is more useful than trying random apostrophe labels until the code compiles.
Read the code
fn longest<'a>(
left: &'a str,
right: &'a str,
) -> &'a str {
if left.len() > right.len() {
left
} else {
right
}
}One name ties the result to both inputs.
Practice starter
fn pick(a: &str, b: &str) -> &str {
if a.len() > b.len() { a } else { b }
}Try it
Describe which input borrow a returned reference may depend on.
What success looks like: You can explain why pick needs one lifetime name on both inputs and the result.
Check your understanding
Why does that need an annotation?
Reveal the answer
- Two inputs mean the compiler cannot tell which one the result borrows from
- A &str return always needs an annotation
- Every function that borrows must annotate its lifetimes
With one input reference it is inferred; with two the relationship must be stated.
Go deeper
Common misconception: “Writing 'a makes the borrowed strings live longer.”
'a only says the result is tied to both inputs, and it extends nothing. pick needs that statement because with two inputs the compiler cannot tell which one the result borrows from.
- Link the references:
longestputs'aonleft,right, and the return type, so the result counts as borrowing from both inputs. - Predict before checking: Call
longestwith oneStringfrom an outer scope and one from an inner block. Predict whether the result can be printed inside that block, and after it. - Compare with evidence: Compile both uses: inside the block is accepted, and after the block E0597 says the inner string does not live long enough.
Transfer challenge: Annotate pick the way longest is annotated. Then make its body always return a, drop 'a from b, and check that the result can now outlive the string passed as b.
Connections
- Drop & Scope: Deterministic cleanup without a GC
- Generics: Reusable types and functions
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Lifetime Intuition (courseId "ownership", lessonId "lifetimes-intro") Link: https://crabrix.com/learn/ownership/lifetimes-intro/ Goal: A lifetime annotation relates borrows; it never extends one. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/lifetimes-intro) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.