Lifetime elision is a set of rules the Rust compiler uses to automatically infer reference lifetimes when you don't explicitly write them, preventing dangling references without extra syntax. The three rules are: 1) Each input reference gets its own lifetime, 2) If there is exactly one input lifetime, that lifetime is assigned to all output references, and 3) If there are multiple input lifetimes but one is &self, that lifetime is assigned to all output references.
// Rule 2: One input lifetime assigned to output
fn first_word(s: &str) -> &str {
// Compiler infers: fn first_word<'a>(s: &'a str) -> &'a str
s.split_whitespace().next().unwrap()
}
// Rule 3: &self lifetime assigned to output
impl Text {
fn first_word(&self) -> &str {
// Compiler infers: fn first_word<'a>(&'a self) -> &'a str
self.0.split_whitespace().next().unwrap()
}
}