I'm implementing a trait for a reference type using a Rust older than 1.31. Why does Rust want an explicit lifetime when I tell it what reference type I'm implementing the trait for?
Here's a simple example. A struct Inches
, an implementation of the
Add
trait for &Inches
, and a function that uses that implementation.
Initial example
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 the missing lifetime specifier (still doesn't compile)
// 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 the lifetime on the impl
(now it compiles)
// was: impl Add for &'b Inches {
impl<'b> Add for &'b Inches {
...
}
This, finally, compiles correctly.
My question
Why is
&Inches
inimpl Add for &Inches
considered to lack a lifetime specifier? What problem is solved by telling the compiler that this Add method is for&Inches
with some unspecified non-static lifetime'b
, and then never referring to that lifetime anywhere else?