Trait PartialEq
pub trait PartialEq<Rhs: PointeeSized = Self>: PointeeSized
Trait for comparisons using the equality operator.
Implementing this trait for types provides the == and != operators for
those types.
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 comparisons using the equality operator, 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
[trait@Eq]. Formally speaking, when Rhs == Self, this trait corresponds
to a partial equivalence relation.
Implementations must ensure that eq and ne are consistent with each other:
a != bif and only if!(a == b).
The default implementation of ne provides this consistency and is almost
always sufficient. It should not be overridden without very good reason.
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):
-
Symmetry: if
A: PartialEq<B>andB: PartialEq<A>, thena == bimpliesb == a; and -
Transitivity: if
A: PartialEq<B>andB: PartialEq<C>andA: PartialEq<C>, thena == bandb == cimpliesa == c. This must also work for longer chains, such as whenA: PartialEq<B>,B: PartialEq<C>,C: PartialEq<D>, andA: PartialEq<D>all exist.
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.
Violating these requirements is a logic error. The behavior resulting from a logic error is not
specified, but users of the trait must ensure that such logic errors do not result in
undefined behavior. This means that unsafe code must not rely on the correctness of these
methods.
Cross-crate considerations
Upholding the requirements stated above can become tricky when one crate implements PartialEq
for a type of another crate (i.e., to allow comparing one of its own types with a type from the
standard library). The recommendation is to never implement this trait for a foreign type. In
other words, such a crate should do impl PartialEq<ForeignType> for LocalType, but it should
not do impl PartialEq<LocalType> for ForeignType.
This avoids the problem of transitive chains that criss-cross crate boundaries: for all local
types T, you may assume that no other crate will add impls that allow comparing T == U. In
other words, if other crates add impls that allow building longer transitive chains U1 == ... == T == V1 == ..., then all the types that appear to the right of T must be types that the
crate defining T already knows about. This rules out transitive chains where downstream crates
can add new impls that "stitch together" comparisons of foreign types in ways that violate
transitivity.
Not having such foreign impls also avoids forward compatibility issues where one crate adding
more PartialEq implementations can cause build failures in downstream crates.
Derivable
This trait can be used with #[derive]. When derived on structs, two
instances are equal if all fields are equal, and not equal if any fields
are not equal. When derived 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:
let b1 = Book ;
let b2 = Book ;
let b3 = Book ;
assert!;
assert!;
How 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
// Implement <Book> == <BookFormat> comparisons
// Implement <BookFormat> == <Book> comparisons
let b1 = Book ;
assert!;
assert!;
By changing impl PartialEq for Book to impl PartialEq<BookFormat> for Book,
we allow BookFormats to be compared with Books.
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
}
Examples
let x: u32 = 0;
let y: u32 = 1;
assert_eq!;
assert_eq!;
Required Methods
fn eq(&self, other: &Rhs) -> boolEquality operator
==.Implementation of the "is equal to" operator
==: tests whether its arguments are equal.
Provided Methods
fn ne(&self, other: &Rhs) -> boolInequality operator
!=.Implementation of the "is not equal to" or "is different from" operator
!=: tests whether its arguments are different.Default implementation
The default implementation of the inequality operator simply calls the implementation of the equality operator and negates the result.
This default shouldn't be overridden without good reason, such as when forwarding to another PartialEq implementation.
Implementors
impl PartialEq for ()impl PartialEq for AddrKindimpl PartialEq for AddrParseErrorimpl PartialEq for Alignmentimpl PartialEq for Alignmentimpl PartialEq for AlignmentEnumimpl PartialEq for AllocErrorimpl PartialEq for AsciiCharimpl PartialEq for Assumeimpl PartialEq for AtomicOrderingimpl PartialEq for BiasedFpimpl PartialEq for Big32x40impl PartialEq for Big8x3impl PartialEq for ByteStrimpl PartialEq for CStrimpl PartialEq for CharCaseimpl PartialEq for CharErrorKindimpl PartialEq for CharTryFromErrorimpl PartialEq for CodePointimpl PartialEq for CodePointInnerimpl PartialEq for CpuidResultimpl PartialEq for DebugAsHeximpl PartialEq for Decimalimpl PartialEq for DecimalSeqimpl PartialEq for DecodeUtf16Errorimpl PartialEq for Decodedimpl PartialEq for Durationimpl PartialEq for Errorimpl PartialEq for ErrorKindimpl PartialEq for FieldIdimpl PartialEq for FloatErrorKindimpl PartialEq for FormattingOptionsimpl PartialEq for FpCategoryimpl PartialEq for FromBytesUntilNulErrorimpl PartialEq for FromBytesWithNulErrorimpl PartialEq for FullDecodedimpl PartialEq for GetDisjointMutErrorimpl PartialEq for I32NotAllOnesimpl PartialEq for I64NotAllOnesimpl PartialEq for IndexRangeimpl PartialEq for Infallibleimpl PartialEq for IntErrorKindimpl PartialEq for IpAddrimpl PartialEq for Ipv4Addrimpl PartialEq for Ipv6Addrimpl PartialEq for Ipv6MulticastScopeimpl PartialEq for Layoutimpl PartialEq for LayoutErrorimpl PartialEq for Localityimpl PartialEq for Location<'_>impl PartialEq for Nanosecondsimpl PartialEq for NonZeroCharInnerimpl PartialEq for NonZeroI128Innerimpl PartialEq for NonZeroI16Innerimpl PartialEq for NonZeroI32Innerimpl PartialEq for NonZeroI64Innerimpl PartialEq for NonZeroI8Innerimpl PartialEq for NonZeroIsizeInnerimpl PartialEq for NonZeroU128Innerimpl PartialEq for NonZeroU16Innerimpl PartialEq for NonZeroU32Innerimpl PartialEq for NonZeroU64Innerimpl PartialEq for NonZeroU8Innerimpl PartialEq for NonZeroUsizeInnerimpl PartialEq for Orderingimpl PartialEq for Orderingimpl PartialEq for ParseBoolErrorimpl PartialEq for ParseCharErrorimpl PartialEq for ParseFloatErrorimpl PartialEq for ParseIntErrorimpl PartialEq for PhantomPinnedimpl PartialEq for RangeFullimpl PartialEq for RawWakerimpl PartialEq for RawWakerVTableimpl PartialEq for SearchStepimpl PartialEq for SeekFromimpl PartialEq for Signimpl PartialEq for Signimpl PartialEq for SimdAlignimpl PartialEq for SocketAddrimpl PartialEq for SocketAddrV4impl PartialEq for SocketAddrV6impl PartialEq for TryFromCharErrorimpl PartialEq for TryFromFloatSecsErrorimpl PartialEq for TryFromFloatSecsErrorKindimpl PartialEq for TryFromIntErrorimpl PartialEq for TypeIdimpl PartialEq for U32NotAllOnesimpl PartialEq for U64NotAllOnesimpl PartialEq for UsizeNoHighBitimpl PartialEq for Utf8Errorimpl PartialEq for VariantIdimpl PartialEq for Wtf8impl PartialEq for boolimpl PartialEq for charimpl PartialEq for f128impl PartialEq for f16impl PartialEq for f32impl PartialEq for f64impl PartialEq for i128impl PartialEq for i16impl PartialEq for i32impl PartialEq for i64impl PartialEq for i8impl PartialEq for isizeimpl PartialEq for neverimpl PartialEq for strimpl PartialEq for svpatternimpl PartialEq for svprfopimpl PartialEq for u128impl PartialEq for u16impl PartialEq for u32impl PartialEq for u64impl PartialEq for u8impl PartialEq for usizeimpl PartialEq<&CStr> for CStrimpl PartialEq<&[u8]> for ByteStrimpl PartialEq<&str> for ByteStrimpl PartialEq<ByteStr> for &[u8]impl PartialEq<ByteStr> for &strimpl PartialEq<ByteStr> for [u8]impl PartialEq<ByteStr> for strimpl PartialEq<IpAddr> for Ipv4Addrimpl PartialEq<IpAddr> for Ipv6Addrimpl PartialEq<Ipv4Addr> for IpAddrimpl PartialEq<Ipv6Addr> for IpAddrimpl PartialEq<[u8]> for ByteStrimpl PartialEq<str> for ByteStrimpl<'a> PartialEq for Part<'a>impl<'a> PartialEq for PhantomContravariantLifetime<'a>impl<'a> PartialEq for PhantomCovariantLifetime<'a>impl<'a> PartialEq for PhantomInvariantLifetime<'a>impl<'a> PartialEq for Utf8Chunk<'a>impl<'a> PartialEq for Utf8Pattern<'a>impl<A, B: PointeeSized> PartialEq<&B> for &A where A: ~const PartialEq<B> + PointeeSized,impl<A, B: PointeeSized> PartialEq<&B> for &mut A where A: ~const PartialEq<B> + PointeeSized,impl<A, B: PointeeSized> PartialEq<&mut B> for &A where A: ~const PartialEq<B> + PointeeSized,impl<A, B: PointeeSized> PartialEq<&mut B> for &mut A where A: ~const PartialEq<B> + PointeeSized,impl<A: ~const PartialEq, Z: ~const PartialEq, Y: ~const PartialEq, X: ~const PartialEq, W: ~const PartialEq, V: ~const PartialEq, U: ~const PartialEq, T: ~const PartialEq> PartialEq for (A, Z, Y, X, W, V, U, T)impl<B: ~const PartialEq, A: ~const PartialEq, Z: ~const PartialEq, Y: ~const PartialEq, X: ~const PartialEq, W: ~const PartialEq, V: ~const PartialEq, U: ~const PartialEq, T: ~const PartialEq> PartialEq for (B, A, Z, Y, X, W, V, U, T)impl<B: ~const PartialEq, C: ~const PartialEq> PartialEq for ControlFlow<B, C>impl<C: ~const PartialEq, B: ~const PartialEq, A: ~const PartialEq, Z: ~const PartialEq, Y: ~const PartialEq, X: ~const PartialEq, W: ~const PartialEq, V: ~const PartialEq, U: ~const PartialEq, T: ~const PartialEq> PartialEq for (C, B, A, Z, Y, X, W, V, U, T)impl<D: ~const PartialEq, C: ~const PartialEq, B: ~const PartialEq, A: ~const PartialEq, Z: ~const PartialEq, Y: ~const PartialEq, X: ~const PartialEq, W: ~const PartialEq, V: ~const PartialEq, U: ~const PartialEq, T: ~const PartialEq> PartialEq for (D, C, B, A, Z, Y, X, W, V, U, T)impl<Dyn: PointeeSized> PartialEq for DynMetadata<Dyn>impl<E: ~const PartialEq, D: ~const PartialEq, C: ~const PartialEq, B: ~const PartialEq, A: ~const PartialEq, Z: ~const PartialEq, Y: ~const PartialEq, X: ~const PartialEq, W: ~const PartialEq, V: ~const PartialEq, U: ~const PartialEq, T: ~const PartialEq> PartialEq for (E, D, C, B, A, Z, Y, X, W, V, U, T)impl<F> PartialEq for fn(T) -> Ret where F: FnPtr,impl<F: FnPtr> PartialEq for Fimpl<H> PartialEq for BuildHasherDefault<H>impl<Idx: PartialEq> PartialEq for RangeInclusive<Idx>impl<Idx: PartialEq> PartialEq for RangeToInclusive<Idx>impl<Idx: PartialEq> PartialEq for RangeToInclusive<Idx>impl<Idx: ~const PartialEq> PartialEq for Range<Idx>impl<Idx: ~const PartialEq> PartialEq for Range<Idx>impl<Idx: ~const PartialEq> PartialEq for RangeFrom<Idx>impl<Idx: ~const PartialEq> PartialEq for RangeFrom<Idx>impl<Idx: ~const PartialEq> PartialEq for RangeInclusive<Idx>impl<Idx: ~const PartialEq> PartialEq for RangeTo<Idx>impl<K: PartialEq, V> PartialEq for KeyAndValue<K, V>impl<Ptr: Deref, Q: Deref> PartialEq<Pin<Q>> for Pin<Ptr> where Ptr::Target: PartialEq<Q::Target>,impl<T> PartialEq for Discriminant<T>impl<T> PartialEq for NonZero<T> where T: ZeroablePrimitive + ~const PartialEq,impl<T> PartialEq for PhantomContravariant<T> where T: ?Sized,impl<T> PartialEq for PhantomCovariant<T> where T: ?Sized,impl<T> PartialEq for PhantomInvariant<T> where T: ?Sized,impl<T, U> PartialEq<SyncView<U>> for SyncView<T> where T: Sync + ~const PartialEq<U> + ?Sized, U: Sync + ?Sized,impl<T, U> PartialEq<[U]> for [T] where T: ~const PartialEq<U>,impl<T, U, const N: usize> PartialEq<&[U]> for [T; N] where T: ~const PartialEq<U>,impl<T, U, const N: usize> PartialEq<&mut [U]> for [T; N] where T: ~const PartialEq<U>,impl<T, U, const N: usize> PartialEq<[U; N]> for &[T] where T: ~const PartialEq<U>,impl<T, U, const N: usize> PartialEq<[U; N]> for &mut [T] where T: ~const PartialEq<U>,impl<T, U, const N: usize> PartialEq<[U; N]> for [T; N] where T: ~const PartialEq<U>,impl<T, U, const N: usize> PartialEq<[U; N]> for [T] where T: ~const PartialEq<U>,impl<T, U, const N: usize> PartialEq<[U]> for [T; N] where T: ~const PartialEq<U>,impl<T, const N: usize> PartialEq for Mask<T, N> where T: MaskElement + PartialEq,impl<T, const N: usize> PartialEq for Simd<T, N> where T: SimdElement + PartialEq,impl<T: ?Sized + PartialEq> PartialEq for ManuallyDrop<T>impl<T: ?Sized + PartialEq> PartialEq for RefCell<T>impl<T: ?Sized, const VARIANT: u32, const FIELD: u32> PartialEq for FieldRepresentingType<T, VARIANT, FIELD>impl<T: PartialEq + Copy> PartialEq for Cell<T>impl<T: PartialEq + PointeeSized> PartialEq for TraitImpl<T>impl<T: PartialEq> PartialEq for Complex<T>impl<T: PartialEq> PartialEq for Cursor<T>impl<T: PartialEq> PartialEq for OnceCell<T>impl<T: PartialEq> PartialEq for Poll<T>impl<T: PartialEq> PartialEq for Saturating<T>impl<T: PartialEq> PartialEq for Wrapping<T>impl<T: PointeeSized> PartialEq for *const Timpl<T: PointeeSized> PartialEq for *mut Timpl<T: PointeeSized> PartialEq for NonNull<T>impl<T: PointeeSized> PartialEq for PhantomData<T>impl<T: SimdElement, const N: usize> PartialEq for Simd<T, N>impl<T: SimdElement, const N: usize> PartialEq for SimdM<T, N>impl<T: ~const PartialEq> PartialEq for (T,)impl<T: ~const PartialEq> PartialEq for Bound<T>impl<T: ~const PartialEq> PartialEq for Option<T>impl<T: ~const PartialEq> PartialEq for Reverse<T>impl<T: ~const PartialEq, E: ~const PartialEq> PartialEq for Result<T, E>impl<U: ~const PartialEq, T: ~const PartialEq> PartialEq for (U, T)impl<V: ~const PartialEq, U: ~const PartialEq, T: ~const PartialEq> PartialEq for (V, U, T)impl<W: ~const PartialEq, V: ~const PartialEq, U: ~const PartialEq, T: ~const PartialEq> PartialEq for (W, V, U, T)impl<X: ~const PartialEq, W: ~const PartialEq, V: ~const PartialEq, U: ~const PartialEq, T: ~const PartialEq> PartialEq for (X, W, V, U, T)impl<Y: PartialEq, R: PartialEq> PartialEq for CoroutineState<Y, R>impl<Y: ~const PartialEq, X: ~const PartialEq, W: ~const PartialEq, V: ~const PartialEq, U: ~const PartialEq, T: ~const PartialEq> PartialEq for (Y, X, W, V, U, T)impl<Z: ~const PartialEq, Y: ~const PartialEq, X: ~const PartialEq, W: ~const PartialEq, V: ~const PartialEq, U: ~const PartialEq, T: ~const PartialEq> PartialEq for (Z, Y, X, W, V, U, T)impl<const N: usize> PartialEq<&[u8; N]> for ByteStrimpl<const N: usize> PartialEq<ByteStr> for &[u8; N]impl<const N: usize> PartialEq<ByteStr> for [u8; N]impl<const N: usize> PartialEq<[u8; N]> for ByteStr