4 ms·
> Blech! Having to explicitly borrow temporary values is super gross. You can cut down on a lot of that by implementing basic operations (Add, Mul, etc.) for b
by anderskaseorg 7y ago
> Blech! Having to explicitly borrow temporary values is super gross.
You can cut down on a lot of that by implementing basic operations (Add, Mul, etc.) for both value and reference types.
impl Add<Vector3> for Vector3 { … }
impl Add<&Vector3> for Vector3 { … }
impl Add<Vector3> for &Vector3 { … }
impl Add<&Vector3> for &Vector3 { … }
(The first three can delegate to the last one, unless there are value-specific optimizations you want to apply.)
- crave_ 7y agoThis has irked me as well. Is there an acceptable way to make this less verbose? A crate with a derive macro for example?
- shmerl 7y agoDoes that essentially overload + for it?
- saghm 7y agoYes; Rust uses traits to implement behavior for operators on custom types (see the full list here: https://doc.rust-lang.org/std/ops/#traits https://doc.rust-lang.org/std/ops/#traits)