Rust writes an anonymous lifetime as `'_`. It marks a reference whose specific lifetime name does not need to be written. When a struct already declares its lifetime, use this shorthand in an `impl` block. section 10.2.3
Following the compiler’s suggestion
For `struct Adventurer<'a> { name: &'a str, hit_points: u32 }`, `impl Adventurer` produces an implicit-elision error. Change it to `impl Adventurer<'_>`; the anonymous lifetime tells Rust that the implementation uses the struct’s reference lifetime without naming it. section 10.2.3
struct Adventurer<'a> {
name: &'a str,
hit_points: u32,
}
impl Adventurer<'_> {
fn take_damage(&mut self) {
self.hit_points -= 20;
println!("{} has {} hit points left!", self.name, self.hit_points);
}
}
See the corrected implementation using `Adventurer<'_>`. section 10.2.3
The same shorthand can connect lifetime parameters on traits and structs, while named lifetimes remain useful when their relationships must be stated explicitly. section 10.2.3
