Rust has a feature that allows you to branch the way you compare objects through the and traits .PartialEq
Eq
Deducing from the documentation, the only difference between the two would be that it Eq
adds the reflexive property to the formula.
But then, are there cases of objects that implement PartialEq
with custom code and that require Eq
?
That is, if creating my own implementation of PartialEq
to compare both objects, in what case would it be advisable and even mandatory to use Eq
?
#[derive(PartialEq, Eq)]
enum Color {
Red,
Blue,
}
#[derive(PartialEq, Eq)]
enum Category {
Hatchback,
Truck,
}
struct Car {
color: Color,
category: Category,
}
impl PartialEq for Car {
fn eq(&self, other: &Self) -> bool {
self.category == other.category
}
}
fn main() {
let my_hatch = Car {
color: Color::Blue,
category: Category::Hatchback,
};
let my_truck = Car {
color: Color::Red,
category: Category::Truck,
};
println!("Are those two cars equals? {}", my_hatch == my_truck);
}