Operator Overloading

In Rust, many of the operators can be overloaded via traits. That is, some operators can
be used to accomplish different tasks based on their input arguments. This is possible
because operators are syntactic sugar for method calls. For example, the + operator in
a + b calls the add method (as in a.add(b)). This add method is part of the Add
trait. Hence, the + operator can be used by any implementor of the Add trait.

A list of the traits, such as Add, that overload operators can be found in core::ops.

  1. use std::ops;
  2. struct Foo;
  3. struct Bar;
  4. #[derive(Debug)]
  5. struct FooBar;
  6. #[derive(Debug)]
  7. struct BarFoo;
  8. // The `std::ops::Add` trait is used to specify the functionality of `+`.
  9. // Here, we make `Add<Bar>` - the trait for addition with a RHS of type `Bar`.
  10. // The following block implements the operation: Foo + Bar = FooBar
  11. impl ops::Add<Bar> for Foo {
  12. type Output = FooBar;
  13. fn add(self, _rhs: Bar) -> FooBar {
  14. println!("> Foo.add(Bar) was called");
  15. FooBar
  16. }
  17. }
  18. // By reversing the types, we end up implementing non-commutative addition.
  19. // Here, we make `Add<Foo>` - the trait for addition with a RHS of type `Foo`.
  20. // This block implements the operation: Bar + Foo = BarFoo
  21. impl ops::Add<Foo> for Bar {
  22. type Output = BarFoo;
  23. fn add(self, _rhs: Foo) -> BarFoo {
  24. println!("> Bar.add(Foo) was called");
  25. BarFoo
  26. }
  27. }
  28. fn main() {
  29. println!("Foo + Bar = {:?}", Foo + Bar);
  30. println!("Bar + Foo = {:?}", Bar + Foo);
  31. }

See Also

Add, Syntax Index