I want to write some general mathematical functions without assuming that my types can be copied. This seems impossible, because mathematical operators consume values ββinstead of borrowing them. Therefore, you need to copy the object, just for simple math. I could just move them, but this is not possible in the borrowed context that I need when I want to mutate the structure.
Here is a simple example where there are problems:
use std::ops::Add;
struct NoCopy<T>(T);
struct Cont<T>(NoCopy<T>);
impl<T: Add<Output=T>> Add for NoCopy<T> {
type Output = NoCopy<T>;
fn add(self, x: NoCopy<T>) -> NoCopy<T> {
NoCopy(self.0+x.0)
}
}
fn main() {
let x = NoCopy(1);
let cont = Cont(x);
let rel = NoCopy(2);
cont.0=cont.0+rel;
}
- (, ), , , , , " ".
?
( )?