Lifetimes, references, generics operator overloading impl trouble

struct V2{x:T,y:T}

impl<'a,T> ops::Add<V2> for &'a mut V2{
type Output=&'a mut V2;
fn add(self,_rhs:V2)->&'a mut V2{

Can somebody maybe help explain whats going on here? This seems very much so overly complex or maybe its just something im missing. All i wanted was a Vector2d struct that could use add operation with other Vector2d - specifically to also mutate the A in A+B.

Please use a code block instead of a quote block. You do that using three backticks as outlined below:

this is a code block

and not like this:

> this is a quote block
1 Like

Which part do you think isn't necessary? After fixing the obvious errors (didn't check), I annotated each element of the code:

// definition of the type itself, which takes a type parameter
struct V2<T> {
    x: T,
    y: T,

// implement Add for &mut V2, with any lifetime 'a and any type T
impl<'a,T> ops::Add<V2<T>> for &'a mut V2<T> {
    type Output = &'a mut V2<T>; // where the result type is the same

    // this function will be called by the overloaded operator
    fn add(self, _rhs: V2<T>) -> &'a mut V2<T> {
        self // which apparently just returns the LHS unmodified

There's nothing in this piece of code that's not needed for expressing what you (seem to) want.

Also, please use whitespace.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.