1

I'm not new to Rust, but I am new to using operator overloading in Rust. Say I have a simple Vec3f struct. I want to do something straightforward like:

impl ops::Add<Vec3f> for Vec3f {
    type Output = Vec3f;

    fn add(self, b: Vec3f) -> Vec3f {
        Vec3f {
            x : self.x + b.x,
            y : self.y + b.y,
            z : self.z + b.z
        }
    }
}

However, the compiler complained if one of the variables passed to the operator overload was borrowed. That can be solved by creating a separate overload for when operators are borrowed, but then I have to maintain 4 separate operator overloads depending on the borrowed state of the two variables.

Is there a better way to handle this? Maybe something I haven't considered using generics?

JR Smith
  • 1,186
  • 15
  • 20
  • Does this answer your question? [How to implement idiomatic operator overloading for values and references in Rust?](https://stackoverflow.com/questions/38811387/how-to-implement-idiomatic-operator-overloading-for-values-and-references-in-rus) – joel Mar 23 '20 at 21:49
  • Seems like it. I guess my DuckDuckGo-fu wasn't particularly strong over the last hour. Thanks for the link! – JR Smith Mar 23 '20 at 21:51

0 Answers0