In Rust, the traits PartialEq and Eq arise from the philosophy of type safety — all fundamental operations for collections (HashMap, HashSet) are based on a strict definition of equality. The traits determine whether objects of two types can be compared using == and whether a type is required to implement "total" equality (Eq) or "partial" equality (PartialEq).
Incorrect implementation of PartialEq/Eq can lead to incorrect operation of collections, where basic mathematical properties are violated: comparison with itself, symmetry, transitivity. Furthermore, an incorrect Eq can lead to objects being "lost" in collections or unexpected duplicates occurring.
PartialEq is implemented for types where comparison might not be possible for all values (for example, f32, where NaN != NaN). Eq is meant only for "strict" (total) relations. User-defined types should implement comparison in strict accordance with these contracts.
Example code:
#[derive(PartialEq, Eq)] struct Point { x: i32, y: i32, } let p1 = Point { x: 1, y: 2 }; let p2 = Point { x: 1, y: 2 }; assert!(p1 == p2); // true
Key features:
Why does the standard library implement only PartialEq for the f32 type, but not Eq?
Because according to IEEE-754, NaN != NaN, i.e., the property x == x is not maintained for all x, which is a necessary property for Eq.
Is it mandatory to explicitly implement Eq if PartialEq is implemented manually?
Yes, if the type supports total equality (x == x is always true), Eq must be manually implemented as well, otherwise some structures will fail to compile with your type.
Can a user implement PartialEq "asymmetrically" (x == y, but y != x)?
Technically yes, but it leads to incorrect behavior of collections and is considered a bug.
** Negative case
A user implemented PartialEq for a type that only compares one field, ignoring the others. As a result, HashSet "loses" duplicates.
Pros:
Cons:
** Positive case
Using #[derive(PartialEq, Eq)] or implementing their own version of comparison, taking into account all business logic, ensuring the required properties.
Pros:
Cons: