I am writing an application in Rust that will use vector arithmetic heavily, and I came across the problem of designing operator overloading for a structure type.
So, I have a vector structure like this:
struct Vector3d {
pub x: f64,
pub y: f64,
pub z: f64,
}
and I want to write something like this:
let x = Vector3d {x: 1.0, y: 0.0, z: 0.0};
let y = Vector3d {x: -1.0, y: 0.0, z: 0.0};
let u = x + y;
As far as I can see, there are three ways to do this:
Contribute std::ops::Addonly for Vector3d. This works, but this signature of the symptom method is:
fn add(self, other: Vector3d)
Thus, it will nullify its arguments after use (because it moves them), which is undesirable in my case, since many vectors will be used in several expressions.
Add Vector3d, Copy. , , , Vector3d - ( , 24 ), , .
Add Vector3d, . , , ,
let u = &x + &y;
, , u = x + y.
, . , : "+" ,
- , ;
u = x + y u = &x + &y?