Comparisons
These traits support comparisons between values. All traits can be derived for types containing fields that implement these traits.
PartialEq
and Eq
PartialEq
is a partial equivalence relation, with required method eq
and
provided method ne
. The ==
and !=
operators will call these methods.
Eq
is a full equivalence relation (reflexive, symmetric, and transitive) and
implies PartialEq
. Functions that require full equivalence will use Eq
as a
trait bound.
PartialOrd
and Ord
PartialOrd
defines a partial ordering, with a partial_cmp
method. It is used
to implement the <
, <=
, >=
, and >
operators.
Ord
is a total ordering, with cmp
returning Ordering
.
Speaker Notes
This slide should take about 5 minutes.
-
PartialEq
can be implemented between different types, butEq
cannot, because it is reflexive: -
In practice, it’s common to derive these traits, but uncommon to implement them.
-
When comparing references in Rust, it will compare the value of the things pointed to, it will NOT compare the references themselves. That means that references to two different things can compare as equal if the values pointed to are the same: