PartialEq

Trait PartialEq 

1.6.0 (const: unstable) · Source
pub trait PartialEq<Rhs: PointeeSized = Self>: PointeeSized {
    // Required method
    fn eq(&self, other: &Rhs) -> bool;

    // Provided method
    fn ne(&self, other: &Rhs) -> bool { ... }
}
Expand description

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 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 != b if 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> and B: PartialEq<A>, then a == b implies b == a; and

  • Transitivity: if A: PartialEq<B> and B: PartialEq<C> and A: PartialEq<C>, then a == b and b == c implies a == c. This must also work for longer chains, such as when A: PartialEq<B>, B: PartialEq<C>, C: PartialEq<D>, and A: 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:

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);

§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
#[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);

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!(x == y, false);
assert_eq!(x.eq(&y), false);

Required Methods§

1.0.0 · Source

fn eq(&self, other: &Rhs) -> bool

Tests for self and other values to be equal, and is used by ==.

Provided Methods§

1.0.0 · Source

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Implementors§

Source§

impl PartialEq for AsciiChar

Available on non-crate feature ferrocene_certified only.
1.34.0 (const: unstable) · Source§

impl PartialEq for Infallible

Available on non-crate feature ferrocene_certified only.
1.64.0 · Source§

impl PartialEq for FromBytesWithNulError

Available on non-crate feature ferrocene_certified only.
1.28.0 · Source§

impl PartialEq for core::fmt::Alignment

Source§

impl PartialEq for DebugAsHex

Source§

impl PartialEq for Sign

Source§

impl PartialEq for AtomicOrdering

1.7.0 · Source§

impl PartialEq for IpAddr

Available on non-crate feature ferrocene_certified only.
Source§

impl PartialEq for Ipv6MulticastScope

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl PartialEq for SocketAddr

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl PartialEq for FpCategory

1.55.0 · Source§

impl PartialEq for IntErrorKind

Available on non-crate feature ferrocene_certified only.
1.86.0 · Source§

impl PartialEq for GetDisjointMutError

Available on non-crate feature ferrocene_certified only.
Source§

impl PartialEq for SearchStep

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl PartialEq for core::sync::atomic::Ordering

1.0.0 (const: unstable) · Source§

impl PartialEq for core::cmp::Ordering

1.0.0 (const: unstable) · Source§

impl PartialEq for bool

1.0.0 (const: unstable) · Source§

impl PartialEq for char

1.0.0 (const: unstable) · Source§

impl PartialEq for f16

1.0.0 (const: unstable) · Source§

impl PartialEq for f32

1.0.0 (const: unstable) · Source§

impl PartialEq for f64

1.0.0 (const: unstable) · Source§

impl PartialEq for f128

1.0.0 (const: unstable) · Source§

impl PartialEq for i8

1.0.0 (const: unstable) · Source§

impl PartialEq for i16

1.0.0 (const: unstable) · Source§

impl PartialEq for i32

1.0.0 (const: unstable) · Source§

impl PartialEq for i64

1.0.0 (const: unstable) · Source§

impl PartialEq for i128

1.0.0 (const: unstable) · Source§

impl PartialEq for isize

Source§

impl PartialEq for !

Available on non-crate feature ferrocene_certified only.
1.0.0 (const: unstable) · Source§

impl PartialEq for str

Available on non-crate feature ferrocene_certified only.
1.0.0 (const: unstable) · Source§

impl PartialEq for u8

1.0.0 (const: unstable) · Source§

impl PartialEq for u16

1.0.0 (const: unstable) · Source§

impl PartialEq for u32

1.0.0 (const: unstable) · Source§

impl PartialEq for u64

1.0.0 (const: unstable) · Source§

impl PartialEq for u128

1.0.0 (const: unstable) · Source§

impl PartialEq for ()

Available on non-crate feature ferrocene_certified only.
1.0.0 (const: unstable) · Source§

impl PartialEq for usize

Source§

impl PartialEq for AllocError

1.28.0 · Source§

impl PartialEq for Layout

1.50.0 · Source§

impl PartialEq for LayoutError

1.0.0 (const: unstable) · Source§

impl PartialEq for TypeId

Available on non-crate feature ferrocene_certified only.
1.27.0 · Source§

impl PartialEq for CpuidResult

Available on x86 or x86-64 only.
Source§

impl PartialEq for ByteStr

Available on non-crate feature ferrocene_certified only.
1.34.0 · Source§

impl PartialEq for CharTryFromError

Available on non-crate feature ferrocene_certified only.
1.9.0 · Source§

impl PartialEq for DecodeUtf16Error

Available on non-crate feature ferrocene_certified only.
1.20.0 · Source§

impl PartialEq for ParseCharError

Available on non-crate feature ferrocene_certified only.
1.59.0 · Source§

impl PartialEq for TryFromCharError

Available on non-crate feature ferrocene_certified only.
1.64.0 · Source§

impl PartialEq for CStr

Available on non-crate feature ferrocene_certified only.
1.69.0 · Source§

impl PartialEq for FromBytesUntilNulError

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl PartialEq for Error

Source§

impl PartialEq for FormattingOptions

1.33.0 · Source§

impl PartialEq for PhantomPinned

Source§

impl PartialEq for Assume

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl PartialEq for AddrParseError

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl PartialEq for Ipv4Addr

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl PartialEq for Ipv6Addr

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl PartialEq for SocketAddrV4

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl PartialEq for SocketAddrV6

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl PartialEq for ParseFloatError

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl PartialEq for ParseIntError

Available on non-crate feature ferrocene_certified only.
1.34.0 · Source§

impl PartialEq for TryFromIntError

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl PartialEq for RangeFull

1.10.0 · Source§

impl PartialEq for Location<'_>

Available on non-crate feature ferrocene_certified only.
Source§

impl PartialEq for core::ptr::Alignment

1.0.0 · Source§

impl PartialEq for ParseBoolError

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl PartialEq for Utf8Error

Available on non-crate feature ferrocene_certified only.
1.36.0 · Source§

impl PartialEq for RawWaker

Available on non-crate feature ferrocene_certified only.
1.36.0 · Source§

impl PartialEq for RawWakerVTable

Available on non-crate feature ferrocene_certified only.
1.3.0 · Source§

impl PartialEq for Duration

Available on non-crate feature ferrocene_certified only.
1.66.0 · Source§

impl PartialEq for TryFromFloatSecsError

Available on non-crate feature ferrocene_certified only.
1.90.0 · Source§

impl PartialEq<&CStr> for CStr

Available on non-crate feature ferrocene_certified only.
1.16.0 · Source§

impl PartialEq<IpAddr> for Ipv4Addr

Available on non-crate feature ferrocene_certified only.
1.16.0 · Source§

impl PartialEq<IpAddr> for Ipv6Addr

Available on non-crate feature ferrocene_certified only.
1.16.0 · Source§

impl PartialEq<Ipv4Addr> for IpAddr

Available on non-crate feature ferrocene_certified only.
1.16.0 · Source§

impl PartialEq<Ipv6Addr> for IpAddr

Available on non-crate feature ferrocene_certified only.
Source§

impl<'a> PartialEq for Utf8Pattern<'a>

Available on non-crate feature ferrocene_certified only.
Source§

impl<'a> PartialEq for PhantomContravariantLifetime<'a>

Available on non-crate feature ferrocene_certified only.
Source§

impl<'a> PartialEq for PhantomCovariantLifetime<'a>

Available on non-crate feature ferrocene_certified only.
Source§

impl<'a> PartialEq for PhantomInvariantLifetime<'a>

Available on non-crate feature ferrocene_certified only.
1.79.0 · Source§

impl<'a> PartialEq for Utf8Chunk<'a>

Available on non-crate feature ferrocene_certified only.
Source§

impl<'a> PartialEq<&str> for ByteStr

Available on non-crate feature ferrocene_certified only.
Source§

impl<'a> PartialEq<&[u8]> for ByteStr

Available on non-crate feature ferrocene_certified only.
Source§

impl<'a> PartialEq<str> for ByteStr

Available on non-crate feature ferrocene_certified only.
Source§

impl<'a> PartialEq<ByteStr> for &str

Available on non-crate feature ferrocene_certified only.
Source§

impl<'a> PartialEq<ByteStr> for &[u8]

Available on non-crate feature ferrocene_certified only.
Source§

impl<'a> PartialEq<ByteStr> for str

Available on non-crate feature ferrocene_certified only.
Source§

impl<'a> PartialEq<ByteStr> for [u8]

Available on non-crate feature ferrocene_certified only.
Source§

impl<'a> PartialEq<[u8]> for ByteStr

Available on non-crate feature ferrocene_certified only.
1.0.0 (const: unstable) · Source§

impl<A, B: PointeeSized> PartialEq<&B> for &A
where A: PartialEq<B> + PointeeSized,

Available on non-crate feature ferrocene_certified only.
1.0.0 (const: unstable) · Source§

impl<A, B: PointeeSized> PartialEq<&B> for &mut A
where A: PartialEq<B> + PointeeSized,

Available on non-crate feature ferrocene_certified only.
1.0.0 (const: unstable) · Source§

impl<A, B: PointeeSized> PartialEq<&mut B> for &A
where A: PartialEq<B> + PointeeSized,

Available on non-crate feature ferrocene_certified only.
1.0.0 (const: unstable) · Source§

impl<A, B: PointeeSized> PartialEq<&mut B> for &mut A
where A: PartialEq<B> + PointeeSized,

Available on non-crate feature ferrocene_certified only.
1.55.0 (const: unstable) · Source§

impl<B: PartialEq, C: PartialEq> PartialEq for ControlFlow<B, C>

Source§

impl<Dyn: PointeeSized> PartialEq for DynMetadata<Dyn>

Available on non-crate feature ferrocene_certified only.
1.4.0 · Source§

impl<F: FnPtr> PartialEq for F

Available on non-crate feature ferrocene_certified only.
1.29.0 · Source§

impl<H> PartialEq for BuildHasherDefault<H>

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl<Idx: PartialEq> PartialEq for core::ops::Range<Idx>

1.0.0 · Source§

impl<Idx: PartialEq> PartialEq for core::ops::RangeFrom<Idx>

1.26.0 · Source§

impl<Idx: PartialEq> PartialEq for core::ops::RangeInclusive<Idx>

1.0.0 · Source§

impl<Idx: PartialEq> PartialEq for RangeTo<Idx>

1.26.0 · Source§

impl<Idx: PartialEq> PartialEq for core::ops::RangeToInclusive<Idx>

Source§

impl<Idx: PartialEq> PartialEq for core::range::Range<Idx>

Available on non-crate feature ferrocene_certified only.
Source§

impl<Idx: PartialEq> PartialEq for core::range::RangeFrom<Idx>

Available on non-crate feature ferrocene_certified only.
Source§

impl<Idx: PartialEq> PartialEq for core::range::RangeInclusive<Idx>

Available on non-crate feature ferrocene_certified only.
Source§

impl<Idx: PartialEq> PartialEq for core::range::RangeToInclusive<Idx>

Available on non-crate feature ferrocene_certified only.
1.41.0 · Source§

impl<Ptr: Deref, Q: Deref> PartialEq<Pin<Q>> for Pin<Ptr>
where Ptr::Target: PartialEq<Q::Target>,

Available on non-crate feature ferrocene_certified only.
Source§

impl<T> PartialEq for PhantomContravariant<T>
where T: ?Sized,

Available on non-crate feature ferrocene_certified only.
Source§

impl<T> PartialEq for PhantomCovariant<T>
where T: ?Sized,

Available on non-crate feature ferrocene_certified only.
Source§

impl<T> PartialEq for PhantomInvariant<T>
where T: ?Sized,

Available on non-crate feature ferrocene_certified only.
1.21.0 · Source§

impl<T> PartialEq for Discriminant<T>

Available on non-crate feature ferrocene_certified only.
1.28.0 (const: unstable) · Source§

impl<T> PartialEq for NonZero<T>

Available on non-crate feature ferrocene_certified only.
1.0.0 (const: unstable) · Source§

impl<T, U> PartialEq<[U]> for [T]
where T: PartialEq<U>,

Available on non-crate feature ferrocene_certified only.
Source§

impl<T, U> PartialEq<Exclusive<U>> for Exclusive<T>
where T: Sync + PartialEq<U> + ?Sized, U: Sync + ?Sized,

Available on non-crate feature ferrocene_certified only.
1.0.0 (const: unstable) · Source§

impl<T, U, const N: usize> PartialEq<&[U]> for [T; N]
where T: PartialEq<U>,

Available on non-crate feature ferrocene_certified only.
1.0.0 (const: unstable) · Source§

impl<T, U, const N: usize> PartialEq<&mut [U]> for [T; N]
where T: PartialEq<U>,

Available on non-crate feature ferrocene_certified only.
1.0.0 (const: unstable) · Source§

impl<T, U, const N: usize> PartialEq<[U; N]> for &[T]
where T: PartialEq<U>,

Available on non-crate feature ferrocene_certified only.
1.0.0 (const: unstable) · Source§

impl<T, U, const N: usize> PartialEq<[U; N]> for &mut [T]
where T: PartialEq<U>,

Available on non-crate feature ferrocene_certified only.
1.0.0 (const: unstable) · Source§

impl<T, U, const N: usize> PartialEq<[U; N]> for [T; N]
where T: PartialEq<U>,

Available on non-crate feature ferrocene_certified only.
1.0.0 (const: unstable) · Source§

impl<T, U, const N: usize> PartialEq<[U; N]> for [T]
where T: PartialEq<U>,

Available on non-crate feature ferrocene_certified only.
1.0.0 (const: unstable) · Source§

impl<T, U, const N: usize> PartialEq<[U]> for [T; N]
where T: PartialEq<U>,

Available on non-crate feature ferrocene_certified only.
Source§

impl<T, const N: usize> PartialEq for Mask<T, N>

Available on non-crate feature ferrocene_certified only.
Source§

impl<T, const N: usize> PartialEq for Simd<T, N>

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl<T: PointeeSized> PartialEq for *const T

Pointer equality is by address, as produced by the <*const T>::addr method.

1.0.0 · Source§

impl<T: PointeeSized> PartialEq for *mut T

Available on non-crate feature ferrocene_certified only.

Pointer equality is by address, as produced by the <*mut T>::addr method.

1.0.0 · Source§

impl<T: PointeeSized> PartialEq for PhantomData<T>

Available on non-crate feature ferrocene_certified only.
1.25.0 · Source§

impl<T: PointeeSized> PartialEq for NonNull<T>

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl<T: PartialEq + Copy> PartialEq for Cell<T>

Available on non-crate feature ferrocene_certified only.
1.20.0 · Source§

impl<T: PartialEq + ?Sized> PartialEq for ManuallyDrop<T>

Available on non-crate feature ferrocene_certified only.
1.17.0 · Source§

impl<T: PartialEq> PartialEq for Bound<T>

1.0.0 (const: unstable) · Source§

impl<T: PartialEq> PartialEq for Option<T>

Available on non-crate feature ferrocene_certified only.
1.36.0 · Source§

impl<T: PartialEq> PartialEq for Poll<T>

Available on non-crate feature ferrocene_certified only.
1.0.0 (const: unstable) · Source§

impl<T: PartialEq> PartialEq for (T₁, T₂, …, Tₙ)

Available on non-crate feature ferrocene_certified only.

This trait is implemented for tuples up to twelve items long.

1.70.0 · Source§

impl<T: PartialEq> PartialEq for OnceCell<T>

Available on non-crate feature ferrocene_certified only.
1.74.0 · Source§

impl<T: PartialEq> PartialEq for Saturating<T>

Available on non-crate feature ferrocene_certified only.
1.0.0 · Source§

impl<T: PartialEq> PartialEq for Wrapping<T>

Available on non-crate feature ferrocene_certified only.
1.19.0 (const: unstable) · Source§

impl<T: PartialEq> PartialEq for Reverse<T>

1.0.0 (const: unstable) · Source§

impl<T: PartialEq, E: PartialEq> PartialEq for Result<T, E>

1.0.0 · Source§

impl<T: ?Sized + PartialEq> PartialEq for RefCell<T>

Available on non-crate feature ferrocene_certified only.
Source§

impl<Y: PartialEq, R: PartialEq> PartialEq for CoroutineState<Y, R>

Available on non-crate feature ferrocene_certified only.
Source§

impl<const N: usize> PartialEq<&[u8; N]> for ByteStr

Available on non-crate feature ferrocene_certified only.
Source§

impl<const N: usize> PartialEq<ByteStr> for &[u8; N]

Available on non-crate feature ferrocene_certified only.
Source§

impl<const N: usize> PartialEq<ByteStr> for [u8; N]

Available on non-crate feature ferrocene_certified only.
Source§

impl<const N: usize> PartialEq<[u8; N]> for ByteStr

Available on non-crate feature ferrocene_certified only.