While reviewing some Rust fundamentals, I stumbled across this excellent video about lifetimes. What caught my attention the most was the “three rules” of lifetime elision – a topic I had some idea about, but I’ve never seen clearly explained.
For reference, they are:
- Each parameter that is a reference gets its own lifetime parameter;
- If there is exactly one input lifetime parameter, that lifetime is assigned to all output lifetime parameters;
- If there are multiple input lifetime parameters, but one of them is
&self
or&mut self
, the lifetime ofself
is assigned to all output lifetime parameters.