Member-only story
Dissecting Rust Traits to Learn Their Secrets
We’ll break down the Add trait to learn how it implements all the trait tools to provide all that flexibility

The Add
trait is one of the most interesting traits in Rust. It is an incredibly basic operation for any programming language but it has a bit of everything that we can find in traits, even operator overloading, so it’s an incredible example to learn how the tools of the traits can be pieced together to offer an amazing flexibility with a perfectly tied functionality. Let’s take a look.
The generic
First of all, we have pub trait Add
, the header of the trait, it specifies that the trait is named Add
and it’s public without restrictions, so this trait can be implemented and used anywhere on a Rust project.
Then we have Rhs
, this is a generic identifier like the commonly used T
. In this case it’s named this way because that acronym means Righ Hand Side, a descriptive way of indicating what is the generic part of this trait, the type that we’ll be adding to the item implementing the Add
. For example, in the addition Counter(0) + 1
the RHS is what is at the right, the one, whose type is an integer (i32, u8, whatever).
An important note of this generic is that it’s not just Rhs
but Rhs = Self
. This means that by default the RHS type will be the same as the item implementing the trait. So, we can implement an addition between two counters just like this.
The function
Our next step is the function defined by the trait, what we have to implement to allow additions with our items. We see that it takes two parameters: self
and rhs
of type Rhs
.
An important point of this is that we have self
and not &self
. That means that if we implement this trait we’ll consume the item when performing the addition. This is like this to give flexibility because we can implement Add
for Counter
if we want it to be consumed, for &Counter
if we…