Built-in traits and operator overloading
The Rust standard library is packed with traits, which are used all over the place. For example, there are traits for which the compiler is capable of providing a basic implementation with a #[derive] attribute, as we saw in the section on Traits:
- Comparing instances: The
EqandPartialEqtrait - Ordering instances: The
OrdandPartialOrdtrait - Creating an empty instance: The
Defaulttrait - To create a zero instance of a numeric data type: The
Zerotrait
The next chapter shows an example of how to implement the following three traits:
- Formatting a value using {
:?}: TheDebugtrait, defining anfmtmethod - Copy an instance: The
Copytrait - Create a duplicate instance: The
Clonetrait - Computing a hash: The
Hashtrait - Adding instances: The
Addtrait, defining anaddmethod. The+operator is just a nice way to useadd: n + mis the same asn.add(m). So if we implement theAddtrait, we can use the+operator, this is called operator overloading.- The
Addtrait has the...
- The