I know we can implement traits that override the standard arithmetic operators. Can we also create our own traits that overload custom operators? I have come to really enjoy Haskell's system for defining operators.
C++ supports operator overloading, but you are not allowed to create your own operators.
Rust allows for a limited form of operator overloading. There are certain operators that are able to be overloaded. To support a particular operator between types, there's a specific trait that you can implement, which then overloads the operator.
No, per the manual the only operators that can be overloaded are:
!— Bitwise or logical complement!=— Nonequality comparison%— Arithmetic remainder%=— Arithmetic remainder and assignment&— Bitwise AND&=— Bitwise AND and assignment*— Arithmetic multiplication*=— Arithmetic multiplication and assignment+— Arithmetic addition+=— Arithmetic addition and assignment-— Arithmetic negation-— Arithmetic subtraction-=— Arithmetic subtraction and assignment/— Arithmetic division/=— Arithmetic division and assignment<<— Left-shift<<=— Left-shift and assignment<— Less than comparison<=— Less than or equal to comparison==— Equality comparison>— Greater than comparison>=— Greater than or equal to comparison>>— Right-shift>>=— Right-shift and assignment^— Bitwise exclusive OR^=— Bitwise exclusive OR and assignment|— Bitwise OR|=— Bitwise OR and assignment
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With