Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we create custom Rust operators?

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.

like image 982
MFlamer Avatar asked May 24 '13 22:05

MFlamer


People also ask

Can you make your own operator?

C++ supports operator overloading, but you are not allowed to create your own operators.

Does rust have operator overloading?

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.


1 Answers

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
like image 125
huon Avatar answered Sep 24 '22 11:09

huon