The Lifetime Elision Rules
The Lifetime Elision Rules
In common function patterns, Rust automatically infers lifetime parameters via three elision rules:
- Each parameter that is a reference gets its own lifetime parameter (
fn foo<'a, 'b>(x: &'a i32, y: &'b i32)). - If there is exactly one input lifetime parameter, that lifetime is assigned to all output lifetime parameters (
fn foo<'a>(x: &'a i32) -> &'a i32). - If there are multiple input lifetime parameters, but one of them is
&selfor&mut self, the lifetime ofselfis assigned to all output lifetime parameters.