I cannot figure out how to have pure looking mathematical expressions for structures without requiring that these structure values be copied everywhere.
If you want to have a structure in which you can do the math, you should write something like this:
use std::ops::*;
struct Num {
i: i32,
}
impl Add for Num {
type Output = Num;
fn add(self, other: Num) -> Num {
Num {
i: self.i + other.i,
}
}
}
(This is a simplified example. Vector math may be the actual example)
This allows you to write good style code a + (b / (c * d)).
Due to the semantics of borrowing, the above code crashes as fast as a + b + a. As soon as aused, as soon as it cannot be used again, since ownership has been transferred to the corresponding function (i.e. add).
Copy :
#[derive(Copy)]
struct Num {
i: i32,
}
, Num add, , .
! : , , , , .
, :
impl<'a> Add for &'a Num {
type Output = Num;
fn add(&'a self, other: &'a Num) -> Num {
Num {
i: self.i + other.i,
}
}
}
, , ! a + (b / (c * d)) &a + &(&b / &(&c * &d)). , (, let a = &Num { /* ... */ }), add - Num.
ops , , struct ?
: