pub trait PartialEq<Rhs: ?Sized = Self> {
fn eq(&self, other: &Rhs) -> bool;
fn ne(&self, other: &Rhs) -> bool { ... }
}
Expand description
Trait for equality comparisons which are partial equivalence relations.
x.eq(y)
can also be written x == y
, and x.ne(y)
can be written x != y
.
We use the easier-to-read infix notation in the remainder of this documentation.
This trait allows for partial equality, for types that do not have a full
equivalence relation. For example, in floating point numbers NaN != NaN
,
so floating point types implement PartialEq
but not Eq
.
Implementations must ensure that eq
and ne
are consistent with each other:
a != b
if and only if!(a == b)
(ensured by the default implementation).
If PartialOrd
or Ord
are also implemented for Self
and Rhs
, their methods must also
be consistent with PartialEq
(see the documentation of those traits for the exact
requirements). It’s easy to accidentally make them disagree by deriving some of the traits and
manually implementing others.
The equality relation ==
must satisfy the following conditions
(for all a
, b
, c
of type A
, B
, C
):
-
Symmetric: if
A: PartialEq<B>
andB: PartialEq<A>
, thena == b
impliesb == a
; and -
Transitive: if
A: PartialEq<B>
andB: PartialEq<C>
andA: PartialEq<C>
, thena == b
andb == c
impliesa == c
.
Note that the B: PartialEq<A>
(symmetric) and A: PartialEq<C>
(transitive) impls are not forced to exist, but these requirements apply
whenever they do exist.
Derivable
This trait can be used with #[derive]
. When derive
d on structs, two
instances are equal if all fields are equal, and not equal if any fields
are not equal. When derive
d on enums, two instances are equal if they
are the same variant and all fields are equal.
How can I implement PartialEq
?
An example implementation for a domain in which two books are considered the same book if their ISBN matches, even if the formats differ:
enum BookFormat {
Paperback,
Hardback,
Ebook,
}
struct Book {
isbn: i32,
format: BookFormat,
}
impl PartialEq for Book {
fn eq(&self, other: &Self) -> bool {
self.isbn == other.isbn
}
}
let b1 = Book { isbn: 3, format: BookFormat::Paperback };
let b2 = Book { isbn: 3, format: BookFormat::Ebook };
let b3 = Book { isbn: 10, format: BookFormat::Paperback };
assert!(b1 == b2);
assert!(b1 != b3);
RunHow can I compare two different types?
The type you can compare with is controlled by PartialEq
’s type parameter.
For example, let’s tweak our previous code a bit:
// The derive implements <BookFormat> == <BookFormat> comparisons
#[derive(PartialEq)]
enum BookFormat {
Paperback,
Hardback,
Ebook,
}
struct Book {
isbn: i32,
format: BookFormat,
}
// Implement <Book> == <BookFormat> comparisons
impl PartialEq<BookFormat> for Book {
fn eq(&self, other: &BookFormat) -> bool {
self.format == *other
}
}
// Implement <BookFormat> == <Book> comparisons
impl PartialEq<Book> for BookFormat {
fn eq(&self, other: &Book) -> bool {
*self == other.format
}
}
let b1 = Book { isbn: 3, format: BookFormat::Paperback };
assert!(b1 == BookFormat::Paperback);
assert!(BookFormat::Ebook != b1);
RunBy changing impl PartialEq for Book
to impl PartialEq<BookFormat> for Book
,
we allow BookFormat
s to be compared with Book
s.
A comparison like the one above, which ignores some fields of the struct,
can be dangerous. It can easily lead to an unintended violation of the
requirements for a partial equivalence relation. For example, if we kept
the above implementation of PartialEq<Book>
for BookFormat
and added an
implementation of PartialEq<Book>
for Book
(either via a #[derive]
or
via the manual implementation from the first example) then the result would
violate transitivity:
#[derive(PartialEq)]
enum BookFormat {
Paperback,
Hardback,
Ebook,
}
#[derive(PartialEq)]
struct Book {
isbn: i32,
format: BookFormat,
}
impl PartialEq<BookFormat> for Book {
fn eq(&self, other: &BookFormat) -> bool {
self.format == *other
}
}
impl PartialEq<Book> for BookFormat {
fn eq(&self, other: &Book) -> bool {
*self == other.format
}
}
fn main() {
let b1 = Book { isbn: 1, format: BookFormat::Paperback };
let b2 = Book { isbn: 2, format: BookFormat::Paperback };
assert!(b1 == BookFormat::Paperback);
assert!(BookFormat::Paperback == b2);
// The following should hold by transitivity but doesn't.
assert!(b1 == b2); // <-- PANICS
}
RunExamples
let x: u32 = 0;
let y: u32 = 1;
assert_eq!(x == y, false);
assert_eq!(x.eq(&y), false);
RunRequired Methods
Provided Methods
Implementors
impl PartialEq<Infallible> for Infallible
impl PartialEq<Alignment> for Alignment
impl PartialEq<FpCategory> for FpCategory
impl PartialEq<IntErrorKind> for IntErrorKind
impl PartialEq<Which> for Which
impl PartialEq<SearchStep> for SearchStep
impl PartialEq<Ordering> for core::sync::atomic::Ordering
impl PartialEq<Ordering> for core::cmp::Ordering
impl PartialEq<bool> for bool
impl PartialEq<char> for char
impl PartialEq<f32> for f32
impl PartialEq<f64> for f64
impl PartialEq<i8> for i8
impl PartialEq<i16> for i16
impl PartialEq<i32> for i32
impl PartialEq<i64> for i64
impl PartialEq<i128> for i128
impl PartialEq<isize> for isize
impl PartialEq<!> for !
impl PartialEq<str> for str
impl PartialEq<u8> for u8
impl PartialEq<u16> for u16
impl PartialEq<u32> for u32
impl PartialEq<u64> for u64
impl PartialEq<u128> for u128
impl PartialEq<()> for ()
impl PartialEq<usize> for usize
impl PartialEq<AllocError> for AllocError
impl PartialEq<Layout> for Layout
impl PartialEq<LayoutError> for LayoutError
impl PartialEq<TypeId> for TypeId
impl PartialEq<CpuidResult> for CpuidResult
impl PartialEq<CharTryFromError> for CharTryFromError
impl PartialEq<DecodeUtf16Error> for DecodeUtf16Error
impl PartialEq<ParseCharError> for ParseCharError
impl PartialEq<TryFromCharError> for TryFromCharError
impl PartialEq<CStr> for CStr
impl PartialEq<FromBytesUntilNulError> for FromBytesUntilNulError
impl PartialEq<FromBytesWithNulError> for FromBytesWithNulError
impl PartialEq<Error> for Error
impl PartialEq<PhantomPinned> for PhantomPinned
impl PartialEq<NonZeroI8> for NonZeroI8
impl PartialEq<NonZeroI16> for NonZeroI16
impl PartialEq<NonZeroI32> for NonZeroI32
impl PartialEq<NonZeroI64> for NonZeroI64
impl PartialEq<NonZeroI128> for NonZeroI128
impl PartialEq<NonZeroIsize> for NonZeroIsize
impl PartialEq<NonZeroU8> for NonZeroU8
impl PartialEq<NonZeroU16> for NonZeroU16
impl PartialEq<NonZeroU32> for NonZeroU32
impl PartialEq<NonZeroU64> for NonZeroU64
impl PartialEq<NonZeroU128> for NonZeroU128
impl PartialEq<NonZeroUsize> for NonZeroUsize
impl PartialEq<ParseFloatError> for ParseFloatError
impl PartialEq<ParseIntError> for ParseIntError
impl PartialEq<TryFromIntError> for TryFromIntError
impl PartialEq<RangeFull> for RangeFull
impl PartialEq<ParseBoolError> for ParseBoolError
impl PartialEq<Utf8Error> for Utf8Error
impl PartialEq<RawWaker> for RawWaker
impl PartialEq<RawWakerVTable> for RawWakerVTable
impl PartialEq<Duration> for Duration
impl PartialEq<FromFloatSecsError> for FromFloatSecsError
impl<'a> PartialEq<Location<'a>> for Location<'a>
impl<'a> PartialEq<Utf8LossyChunk<'a>> for Utf8LossyChunk<'a>
impl<A, B> PartialEq<[B]> for [A] where
A: PartialEq<B>,
impl<A, B, const N: usize> PartialEq<&[B]> for [A; N] where
A: PartialEq<B>,
impl<A, B, const N: usize> PartialEq<&mut [B]> for [A; N] where
A: PartialEq<B>,
impl<A, B, const N: usize> PartialEq<[A; N]> for &[B] where
B: PartialEq<A>,
impl<A, B, const N: usize> PartialEq<[A; N]> for &mut [B] where
B: PartialEq<A>,
impl<A, B, const N: usize> PartialEq<[A; N]> for [B] where
B: PartialEq<A>,
impl<A, B, const N: usize> PartialEq<[B; N]> for [A; N] where
A: PartialEq<B>,
impl<A, B, const N: usize> PartialEq<[B]> for [A; N] where
A: PartialEq<B>,
impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &A where
A: PartialEq<B>,
impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &mut A where
A: PartialEq<B>,
impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &A where
A: PartialEq<B>,
impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &mut A where
A: PartialEq<B>,
impl<B: PartialEq, C: PartialEq> PartialEq<ControlFlow<B, C>> for ControlFlow<B, C>
impl<Dyn: ?Sized> PartialEq<DynMetadata<Dyn>> for DynMetadata<Dyn>
impl<H> PartialEq<BuildHasherDefault<H>> for BuildHasherDefault<H>
impl<Idx: PartialEq> PartialEq<Range<Idx>> for Range<Idx>
impl<Idx: PartialEq> PartialEq<RangeFrom<Idx>> for RangeFrom<Idx>
impl<Idx: PartialEq> PartialEq<RangeInclusive<Idx>> for RangeInclusive<Idx>
impl<Idx: PartialEq> PartialEq<RangeTo<Idx>> for RangeTo<Idx>
impl<Idx: PartialEq> PartialEq<RangeToInclusive<Idx>> for RangeToInclusive<Idx>
impl<P: Deref, Q: Deref> PartialEq<Pin<Q>> for Pin<P> where
P::Target: PartialEq<Q::Target>,
impl<Ret, T> PartialEq<fn(T) -> Ret> for fn (T₁, T₂, …, Tₙ) -> Ret
This trait is implemented for function pointers with up to twelve arguments.
impl<Ret, T> PartialEq<extern "C" fn(T) -> Ret> for extern "C" fn (T₁, T₂, …, Tₙ) -> Ret
This trait is implemented for function pointers with up to twelve arguments.
impl<Ret, T> PartialEq<extern "C" fn(T, ...) -> Ret> for extern "C" fn (T₁, T₂, …, Tₙ, ...) -> Ret
This trait is implemented for function pointers with up to twelve arguments.
impl<Ret, T> PartialEq<unsafe fn(T) -> Ret> for unsafe fn (T₁, T₂, …, Tₙ) -> Ret
This trait is implemented for function pointers with up to twelve arguments.
impl<Ret, T> PartialEq<unsafe extern "C" fn(T) -> Ret> for unsafe extern "C" fn (T₁, T₂, …, Tₙ) -> Ret
This trait is implemented for function pointers with up to twelve arguments.
impl<Ret, T> PartialEq<unsafe extern "C" fn(T, ...) -> Ret> for unsafe extern "C" fn (T₁, T₂, …, Tₙ, ...) -> Ret
This trait is implemented for function pointers with up to twelve arguments.
impl<T> PartialEq<Discriminant<T>> for Discriminant<T>
impl<T, const LANES: usize> PartialEq<Mask<T, LANES>> for Mask<T, LANES> where
T: MaskElement + PartialEq,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> PartialEq<Simd<T, LANES>> for Simd<T, LANES> where
LaneCount<LANES>: SupportedLaneCount,
T: SimdElement + PartialEq,
impl<T: PartialEq + Copy> PartialEq<Cell<T>> for Cell<T>
impl<T: PartialEq + ?Sized> PartialEq<ManuallyDrop<T>> for ManuallyDrop<T>
impl<T: PartialEq> PartialEq<Bound<T>> for Bound<T>
impl<T: PartialEq> PartialEq<Option<T>> for Option<T>
impl<T: PartialEq> PartialEq<Poll<T>> for Poll<T>
impl<T: PartialEq> PartialEq<(T,)> for (T₁, T₂, …, Tₙ) where
T: ?Sized,
This trait is implemented for tuples up to twelve items long.