我正在写一个光线跟踪器,我想能够减去我的三维向量:
use std::ops::Sub;
#[derive(Clone, Debug)]
pub struct Vec3 {
pub v: [f64; 3],
}
impl Sub for Vec3 {
type Output = Vec3;
fn sub(self, other: Vec3) -> Vec3 {
Vec3 {
v: [
self.v[0] - other.v[0],
self.v[1] - other.v[1],
self.v[2] - other.v[2],
],
}
}
}
这似乎奏效了。但是,当我尝试使用它时:
fn main() {
let x = Vec3 { v: [0., 0., 0.] };
let y = Vec3 { v: [0., 0., 0.] };
let a = x - y;
let b = x - y;
}
我收到编者的投诉:
error[E0382]: use of moved value: `x`
--> src/main.rs:26:13
|
25 | let a = x - y;
| - value moved here
26 | let b = x - y;
| ^ value used here after move
|
= note: move occurs because `x` has type `Vec3`, which does not implement the `Copy` trait
error[E0382]: use of moved value: `y`
--> src/main.rs:26:17
|
25 | let a = x - y;
| - value moved here
26 | let b = x - y;
| ^ value used here after move
|
= note: move occurs because `y` has type `Vec3`, which does not implement the `Copy` trait
如何编写减法运算符以使上面的代码工作?
请不要告诉我应该有一个现有的三维数学模块。我确信还有更好的东西,但我是在学习如何自己去学习语言之后。
How do I implement the Add trait for a reference to a struct?
没有帮助,因为它需要为我还没有指定的对象指定生命周期。