Why are there two types of Traits in Rust, Eq and PartialEq?

Beck Moulton
3 min readAug 6, 2024

At first, I had a hard time understanding why Rust overloads the == and! = operators with Eq and PartialEq, and what are their different uses?

First, any type T can implement PartialEq if the following properties are satisfied:

  • Symmetry: a == b means b == a
  • Transitive: a == b and b == c mean a == c

Type T implements Eq, in addition to satisfying the above properties, it also needs to satisfy the following properties:

  • Reflexivity: a == a

Therefore, Eq is a subtrait of PartialEq, which means that type T must implement PartialEq before implementing Eq.

But why doesn’t the Eq trait provide any methods, or is it just a tag? What role does the Eq trait play?

Suppose we want to represent a student. We can create a simple structure:

struct Student {
id: u64,
address: String,
}

Now, we want to compare two student instances. If they have matching IDs, even if they have different addresses (multiple students can have the same address), we assume that the two student instances are equal. Therefore, we can implement the PartialEq trait for the Student type.

According to current requirements, the Student type satisfies all three attributes mentioned above, so the Eq trait can also be implemented for the Student type.

impl PartialEq for

--

--

Beck Moulton

Focus on the back-end field, do actual combat technology sharing Buy me a Coffee if You Appreciate My Hard Work https://www.buymeacoffee.com/BeckMoulton