You need to implement Add
on &Vector
rather than on Vector
.
impl<'a, 'b> Add<&'b Vector> for &'a Vector {
type Output = Vector;
fn add(self, other: &'b Vector) -> Vector {
Vector {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
In its definition, Add::add
always takes self
by value. But references are types like any other1, so they can implement traits too. When a trait is implemented on a reference type, the type of self
is a reference; the reference is passed by value. Normally, passing by value in Rust implies transferring ownership, but when references are passed by value, they're simply copied (or reborrowed/moved if it's a mutable reference), and that doesn't transfer ownership of the referent (because a reference doesn't own its referent in the first place). Considering all this, it makes sense for Add::add
(and many other operators) to take self
by value: if you need to take ownership of the operands, you can implement Add
on structs/enums directly, and if you don't, you can implement Add
on references.
Here, self
is of type &'a Vector
, because that's the type we're implementing Add
on.
Note that I also specified the RHS
type parameter with a different lifetime to emphasize the fact that the lifetimes of the two input parameters are unrelated.
1 Actually, reference types are special in that you can implement traits for references to types defined in your crate (i.e. if you're allowed to implement a trait for T
, then you're also allowed to implement it for &T
). &mut T
and Box<T>
have the same behavior, but that's not true in general for U<T>
where U
is not defined in the same crate.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…