I implement a trait for a reference type using Rust older than 1.31. Why does Rust want an explicit lifetime when I tell it what reference type I am implementing the trait for?
Here is a simple example. The Inches structure, the implementation of the Add attribute for &Inches and the function that uses this implementation.
Initial example
(Rust playground link)
use std::ops::Add; struct Inches(i32); // this would work: impl<'b> Add for &'b Inches impl Add for &Inches { type Output = Inches; fn add(self, other: &Inches) -> Inches { let &Inches(x) = self; let &Inches(y) = other; Inches(x + y) } } // lifetime specifier needed here because otherwise // 'total = hilt + blade' doesn't know whether 'total' should live // as long as 'hilt', or as long as 'blade'. fn add_inches<'a>(hilt: &'a Inches, blade: &'a Inches) { let total = hilt + blade; let Inches(t) = total; println!("length {}", t); } fn main() { let hilt = Inches(10); let blade = Inches(20); add_inches(&hilt, &blade); }
Compilation fails with the following error:
error: missing lifetime specifier [E0106] impl Add for &Inches { ^~~~~~~
I add a missing lifetime indicator (still not compiling)
// was: impl Add for &Inches { impl Add for &'b Inches { ... }
Compilation Error:
error: use of undeclared lifetime name ''b' [E0261] impl Add for &'b Inches {
I declare a lifetime on impl (now it compiles)
(Rust playground link)
// was: impl Add for &'b Inches { impl<'b> Add for &'b Inches { ... }
It finally compiles correctly.
My question
Why don't &Inches in impl Add for &Inches have a lifetime specifier? What problem is solved by telling the compiler that this Add method is for &Inches with some indefinite non-static lifetime 'b , and then never refers to this lifetime elsewhere?
source share