Recall & Review
beginner
What is the purpose of lifetime elision in Rust?
Lifetime elision allows Rust to infer lifetimes in function signatures without explicit annotations, making code easier to read and write.
Click to reveal answer
beginner
How many lifetime elision rules does Rust have?
Rust has three main lifetime elision rules that guide how lifetimes are inferred in function signatures.
Click to reveal answer
intermediate
Explain the first lifetime elision rule.
If there is exactly one input lifetime, that lifetime is assigned to all output lifetimes.
Click to reveal answer
intermediate
What does the second lifetime elision rule state?
If there are multiple input lifetimes, but one of them is &self or &mut self, then the lifetime of self is assigned to all output lifetimes.
Click to reveal answer
intermediate
When do you need to explicitly write lifetime annotations in Rust functions?
You must write explicit lifetime annotations when the elision rules do not apply, such as when multiple input lifetimes exist without a self reference and the output lifetime is ambiguous.
Click to reveal answer
How many lifetime elision rules does Rust use to infer lifetimes?
✗ Incorrect
Rust uses three lifetime elision rules to infer lifetimes automatically.
According to the first elision rule, if a function has one input lifetime, what happens to the output lifetime?
✗ Incorrect
The output lifetime is assigned the same lifetime as the single input lifetime.
What special input does the second elision rule consider when assigning output lifetimes?
✗ Incorrect
The second rule applies when one input is &self or &mut self.
When must you write explicit lifetime annotations in Rust functions?
✗ Incorrect
Explicit annotations are needed when elision rules cannot determine output lifetimes.
Which of these is NOT a lifetime elision rule?
✗ Incorrect
Option A is not a lifetime elision rule; without a self parameter and with multiple input lifetimes, explicit lifetime annotations are required.
Describe the three lifetime elision rules in Rust and give a simple example for each.
Think about function inputs and outputs and how Rust guesses lifetimes.
You got /4 concepts.
Explain when and why you need to write explicit lifetime annotations instead of relying on elision.
Consider functions with multiple references as inputs.
You got /3 concepts.