misc/process/wait.html)
    9. [ **20.6.** Filesystem Operations ](../std_misc/fs.html)
    10. [ **20.7.** Program arguments ](../std_misc/arg.html) â± 
    11.       1. [ **20.7.1.** Argument parsing ](../std_misc/arg/matching.html)
    12. [ **20.8.** Foreign Function Interface ](../std_misc/ffi.html)
  41. [ **21.** Testing ](../testing.html) â± 
  42.     1. [ **21.1.** Unit testing ](../testing/unit_testing.html)
    2. [ **21.2.** Documentation testing ](../testing/doc_testing.html)
    3. [ **21.3.** Integration testing ](../testing/integration_testing.html)
    4. [ **21.4.** Dev-dependencies ](../testing/dev_dependencies.html)
  43. [ **22.** Unsafe Operations ](../unsafe.html) â± 
  44.     1. [ **22.1.** Inline assembly ](../unsafe/asm.html)
  45. [ **23.** Compatibility ](../compatibility.html) â± 
  46.     1. [ **23.1.** Raw identifiers ](../compatibility/raw_identifiers.html)
  47. [ **24.** Meta ](../meta.html) â± 
  48.     1. [ **24.1.** Documentation ](../meta/doc.html)
    2. [ **24.2.** Playground ](../meta/playground.html)

__ __

  * Light (default) 
  * Rust 
  * Coal 
  * Navy 
  * Ayu 

__

#  Rust By Example

[ __ ](../print.html "Print this book") [ __ ](https://github.com/rust-
lang/rust-by-example "Git repository") [ __ ](https://github.com/rust-
lang/rust-by-example/edit/master/src/trait/ops.md "Suggest an edit")

#  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 ` ](https://doc.rust-lang.org/core/ops/) .

    
    
    use std::ops;
    
    struct Foo;
    struct Bar;
    
    #[derive(Debug)]
    struct FooBar;
    
    #[derive(Debug)]
    struct BarFoo;
    
    // The `std::ops::Add` trait is used to specify the functionality of `+`.
    // Here, we make `Add<Bar>` - the trait for addition with a RHS of type `Bar`.
    // The following block implements the operation: Foo + Bar = FooBar
    impl ops::Add<Bar> for Foo {
        type Output = FooBar;
    
        fn add(self, _rhs: Bar) -> FooBar {
            println!("> Foo.add(Bar) was called");
    
            FooBar
        }
    }
    
    // By reversing the types, we end up implementing non-commutative addition.
    // Here, we make `Add<Foo>` - the trait for addition with a RHS of type `Foo`.
    // This block implements the operation: Bar + Foo = BarFoo
    impl ops::Add<Foo> for Bar {
        type Output = BarFoo;
    
        fn add(self, _rhs: Foo) -> BarFoo {
            println!("> Bar.add(Foo) was called");
    
            BarFoo
        }
    }
    
    fn main() {
        println!("Foo + Bar = {:?}", Foo + Bar);
        println!("Bar + Foo = {:?}", Bar + Foo);
    }
    

###  See Also

[ Add ](https://doc.rust-lang.org/core/ops/trait.Add.html) , [ Syntax Index
](https://doc.rust-lang.org/book/appendix-02-operators.html)

[ __ ](../trait/dyn.html "Previous chapter") [ __ ](../trait/drop.html "Next
chapter")

[ __ ](../trait/dyn.html "Previous chapter") [ __ ](../trait/drop.html "Next
chapter")