首页 > 解决方案 > 如何编写支持+=操作的特征绑定,其右手是Rust中复杂情况下的引用

问题描述

我想写一个支持添加操作的 Vector 结构,并写一个使用 Vector 结构的一些特征,所以我写了这个。

use std::ops::*;
#[derive(Clone)]
struct Vector<T>(Vec<T>);

impl<'a, T> Add<&'a Vector<T>> for Vector<T>
where
    T: AddAssign<&'a T>,
{
    type Output = Vector<T>;
    fn add(mut self, rhs: &'a Vector<T>) -> Self::Output {
        self.0
            .iter_mut()
            .zip(rhs.0.iter())
            .for_each(|(left, right)| {
                *left += right;
            });
        self
    }
}

trait SomeOperation<'a ,T>
where
    T: AddAssign<&'a T>+Clone + 'a,
{
    fn add(u:Vector<T>,v:&'a Vector<T>)->Vector<T>{
        let w = u+v;
        let x = v.clone()+&w;
        x
    }
}

但是会出现编译错误。

21 | trait SomeOperation<'a ,T>
   |                     -- lifetime `'a` defined here
...
27 |         let x = v.clone()+&w;
   |                           ^^
   |                           |
   |                           borrowed value does not live long enough
   |                           requires that `w` is borrowed for `'a`
28 |         x
29 |     }
   |     - `w` dropped here while still borrowed

我怎样才能避免这些类型的错误。

标签: rustoverloadingtraitsoperator-keyword

解决方案


您谈论实现AddAssign,但您的代码试图实现Add. 此外,我无法弄清楚SomeOperation是为了什么。我将Debug特征添加到派生线。

use std::ops::*;
#[derive(Clone, Debug)]
struct Vector<T>(Vec<T>);

impl<'a, T> AddAssign<&'a Vector<T>> for Vector<T>
where
    T: AddAssign<&'a T>
{
    fn add_assign(&mut self, rhs: &'a Vector<T>) {
        self.0
            .iter_mut()
            .zip(rhs.0.iter())
            .for_each(|(left, right)| {
                *left += right;
            });
    }
}
impl<'a, 'b, T> Add<& 'b Vector<T>> for & 'a Vector<T>
where
    Vector<T>: AddAssign<& 'b Vector<T>>,
    T: Clone,
{
    type Output = Vector<T>;
    fn add(self, other: & 'b Vector<T>) -> Self::Output {
        let mut res: Vector<T> = self.clone();
        res += other;
        res
    }
}

fn main() {
    let mut v1: Vector<u32> = Vector(vec![1, 2, 3]);
    let v2 = Vector(vec![4, 5, 6]);
    println!("Add:     {:?}", &v1 + &v2);
    v1 += &v2;
    println!("AddAssign{:?}", v1);
}

推荐阅读