Understanding implicit lifetime elision in Rust
This is an easy Rust question that tests whether you understand the compiler's lifetime-elision rules. Rust allows you to omit explicit lifetime annotations in many common cases, and this question probes whether you can explain why a particular piece of code compiles without you writing out &'a or similar syntax.
The key to answering questions like this is knowing the three elision rules the Rust compiler applies to function signatures and method definitions. Rather than memorizing syntax, you need to recognize the patterns: how the compiler infers output lifetimes from input lifetimes, and when a single implicit lifetime is sufficient to make the code unambiguous.
- The three lifetime-elision rules for function and method signatures
- When the compiler can infer a single lifetime vs. when it requires explicit annotation
- How self references interact with lifetime elision in methods