Start with the idea
a + b calls Add::add, {} calls Display::fmt, and .into() calls Into, which you get for free by implementing From. Because they are normal traits, your own types participate in the same syntax as the built-in ones, and the compiler checks the result type at every use.
Operator syntax in Rust is backed by trait implementations. a + b invokes Add::add, and the implementation selects an associated Output type. In the Money example, adding two money values yields another Money; the newtype prevents an ordinary integer from being treated as money without an explicit conversion. This is useful because a small wrapper can make invalid operations harder to write while keeping familiar arithmetic syntax.
Do not overload an operator merely because you can. Readers expect + to combine values predictably; surprising side effects or hidden I/O make code harder to trust. Decide whether addition consumes operands, borrows them, or needs multiple implementations.
Keep learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.