3 ms·
> I actually like how Rust does this. You don't overload the operators per se. You implement traits with named methods like Add that will allow you to use the o
by thaumasiotes 6y ago
> I actually like how Rust does this. You don't overload the operators per se. You implement traits with named methods like Add that will allow you to use the operators. This, to me, is the best of both worlds.
What's the difference supposed to be? Why do I care whether my "add two abstract data types" method is named "__add__" or "operator+"?
- Igelau 6y agoI don't Rust, but if I had to guess, it helps keep you from defining operators that don't make sense. Like a + that actually means concat. You could still do that, but now you'd be implementing it as something you're calling "add" and that might feel dirty. String concatenation probably isn't the best example, but it's the first one that came to mind.
- CUViper 6y agoThat's a funny example, because Rust's `String` does implement `Add` as concatenation! Many think this was a mistake though. https://doc.rust-lang.org/std/string/struct.String.html#impl-Add%3C%26%27_%20str%3E https://doc.rust-lang.org/std/string/struct.String.html#impl...