添加两个数字而不克隆两者

Adding two numbers without cloning both

use std::ops::Add;
#[derive(Debug)]
pub struct Vec3<N>{
    x: N,
    y: N,
    z: N
}
impl<N> Vec3<N>{
    pub fn new(x: N, y: N , z: N) -> Vec3<N>{
        Vec3{x:x,y:y,z:z}
    }
}
impl<N : Clone + Add<Output=N>> Vec3<N>{
    pub fn  add(&mut self,v: &Vec3<N>){
        self.x = self.x.clone() + v.x.clone();
        self.y = self.y.clone() + v.y.clone();
        self.z = self.z.clone() + v.z.clone();
    }
}
impl<N: Add<Output=N>> Add for Vec3<N>{
    type Output = Vec3<N>;
    fn add(self, v: Vec3<N>) -> Vec3<N>{
        Vec3{x: self.x + v.x
            ,y: self.y + v.y 
            ,z: self.z + v.z} 
    }
}

这让我可以写。

mod vec3;
use vec3::*;
fn main() {
    let mut v1 = Vec3::<f32>::new(1.0,2.0,3.0);
    let v2 = Vec3::<f32>::new(1.0,2.0,3.0);
    v1.add(&v2);
    let v3 = v1 + v2;

    println!("{:?}", v3);
}

这个let v3 = v1 + v2;消耗了v1和v2。但这可能并不总是需要的,所以我添加了另一个带有签名 pub fn add(&mut self,v: &Vec3<N>)

的添加函数

我的问题出在这段代码上

impl<N : Clone + Add<Output=N>> Vec3<N>{
    pub fn  add(&mut self,v: &Vec3<N>){
        self.x = self.x.clone() + v.x.clone();
        self.y = self.y.clone() + v.y.clone();
        self.z = self.z.clone() + v.z.clone();
    }
}

我需要克隆两个向量的值以避免移动。但是我真的很想这样写

self.x = self.x + v.x.clone();self.x += v.x.clone(); 我不明白为什么我必须克隆这两个值。

这是怎么做到的?

目前似乎不可能。原因是缺少 += 运算符重载。

但是原始类型上的 clone() 似乎只是一个空洞,所以我猜它实际上并不重要。

似乎没有重载“+=”运算符的可用方法。但是,如果将 Clone 特征替换为 Copy,则可以避免使用 exclipt "clone"(但是,如果需要,可以将它们一起使用):

impl<N: Copy + Add<Output = N>> Vec3<N> {
    pub fn add(&mut self, v: &Vec3<N>){
        self.x = self.x + v.x;
        self.y = self.y + v.y;
        self.z = self.z + v.z;
    }
}

请注意,您根本不需要调用 "clone"!

这是 Rust 文档的直接引用:

When should my type be Copy?

Generally speaking, if your type can implement Copy, it should. There's one important thing to consider though: if you think your type may not be able to implement Copy in the future, then it might be prudent to not implement Copy. This is because removing Copy is a breaking change: that second example would fail to compile if we made Foo non-Copy.

您可以找到有关复制特征的更多信息 here