core::cmp

Trait Eq

1.6.0 · source
pub trait Eq: PartialEq<Self> { }
Expand description

Trait for comparisons corresponding to equivalence relations.

The primary difference to PartialEq is the additional requirement for reflexivity. A type that implements PartialEq guarantees that for all a, b and c:

  • symmetric: a == b implies b == a and a != b implies !(a == b)
  • transitive: a == b and b == c implies a == c

Eq, which builds on top of PartialEq also implies:

  • reflexive: a == a

This property cannot be checked by the compiler, and therefore Eq is a trait without methods.

Violating this property 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.

Floating point types such as f32 and f64 implement only PartialEq but not Eq because NaN != NaN.

§Derivable

This trait can be used with #[derive]. When derived, because Eq has no extra methods, it is only informing the compiler that this is an equivalence relation rather than a partial equivalence relation. Note that the derive strategy requires all fields are Eq, which isn’t always desired.

§How can I implement Eq?

If you cannot use the derive strategy, specify that your type implements Eq, which has no extra methods:

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
    }
}

impl Eq for Book {}

Object Safety§

This trait is not object safe.

Implementors§

source§

impl Eq for AsciiChar

1.34.0 · source§

impl Eq for Infallible

1.28.0 · source§

impl Eq for core::fmt::Alignment

1.7.0 · source§

impl Eq for IpAddr

source§

impl Eq for Ipv6MulticastScope

1.0.0 · source§

impl Eq for SocketAddr

1.0.0 · source§

impl Eq for FpCategory

1.55.0 · source§

impl Eq for IntErrorKind

source§

impl Eq for SearchStep

1.0.0 · source§

impl Eq for core::sync::atomic::Ordering

1.0.0 · source§

impl Eq for core::cmp::Ordering

1.0.0 · source§

impl Eq for bool

1.0.0 · source§

impl Eq for char

1.0.0 · source§

impl Eq for i8

1.0.0 · source§

impl Eq for i16

1.0.0 · source§

impl Eq for i32

1.0.0 · source§

impl Eq for i64

1.0.0 · source§

impl Eq for i128

1.0.0 · source§

impl Eq for isize

source§

impl Eq for !

1.0.0 · source§

impl Eq for str

1.0.0 · source§

impl Eq for u8

1.0.0 · source§

impl Eq for u16

1.0.0 · source§

impl Eq for u32

1.0.0 · source§

impl Eq for u64

1.0.0 · source§

impl Eq for u128

1.0.0 · source§

impl Eq for ()

1.0.0 · source§

impl Eq for usize

source§

impl Eq for AllocError

1.28.0 · source§

impl Eq for Layout

1.50.0 · source§

impl Eq for LayoutError

1.0.0 · source§

impl Eq for TypeId

1.27.0 · source§

impl Eq for CpuidResult

Available on x86 or x86-64 only.
1.34.0 · source§

impl Eq for CharTryFromError

1.9.0 · source§

impl Eq for DecodeUtf16Error

1.20.0 · source§

impl Eq for ParseCharError

1.59.0 · source§

impl Eq for TryFromCharError

1.69.0 · source§

impl Eq for FromBytesUntilNulError

1.64.0 · source§

impl Eq for FromBytesWithNulError

1.64.0 · source§

impl Eq for CStr

1.0.0 · source§

impl Eq for Error

1.33.0 · source§

impl Eq for PhantomPinned

source§

impl Eq for Assume

1.0.0 · source§

impl Eq for AddrParseError

1.0.0 · source§

impl Eq for Ipv4Addr

1.0.0 · source§

impl Eq for Ipv6Addr

1.0.0 · source§

impl Eq for SocketAddrV4

1.0.0 · source§

impl Eq for SocketAddrV6

1.0.0 · source§

impl Eq for ParseFloatError

1.0.0 · source§

impl Eq for ParseIntError

1.34.0 · source§

impl Eq for TryFromIntError

1.0.0 · source§

impl Eq for RangeFull

source§

impl Eq for core::ptr::Alignment

1.0.0 · source§

impl Eq for ParseBoolError

1.0.0 · source§

impl Eq for Utf8Error

1.3.0 · source§

impl Eq for Duration

1.66.0 · source§

impl Eq for TryFromFloatSecsError

1.10.0 · source§

impl<'a> Eq for Location<'a>

1.79.0 · source§

impl<'a> Eq for Utf8Chunk<'a>

1.0.0 · source§

impl<A> Eq for &A
where A: Eq + ?Sized,

1.0.0 · source§

impl<A> Eq for &mut A
where A: Eq + ?Sized,

1.55.0 · source§

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

source§

impl<Dyn: ?Sized> Eq for DynMetadata<Dyn>

1.4.0 · source§

impl<F: FnPtr> Eq for F

1.29.0 · source§

impl<H> Eq for BuildHasherDefault<H>

1.0.0 · source§

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

1.0.0 · source§

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

1.26.0 · source§

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

1.0.0 · source§

impl<Idx: Eq> Eq for RangeTo<Idx>

1.26.0 · source§

impl<Idx: Eq> Eq for RangeToInclusive<Idx>

source§

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

source§

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

source§

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

1.41.0 · source§

impl<Ptr: Deref<Target: Eq>> Eq for Pin<Ptr>

1.0.0 · source§

impl<T> Eq for (T₁, T₂, …, Tₙ)
where T: ?Sized + Eq,

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

1.21.0 · source§

impl<T> Eq for Discriminant<T>

1.28.0 · source§

impl<T> Eq for NonZero<T>
where T: ZeroablePrimitive + Eq,

source§

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

1.2.0 · source§

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

1.20.0 · source§

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

1.17.0 · source§

impl<T: Eq> Eq for Bound<T>

1.0.0 · source§

impl<T: Eq> Eq for Option<T>

1.36.0 · source§

impl<T: Eq> Eq for Poll<T>

1.0.0 · source§

impl<T: Eq> Eq for [T]

1.70.0 · source§

impl<T: Eq> Eq for OnceCell<T>

1.74.0 · source§

impl<T: Eq> Eq for Saturating<T>

1.0.0 · source§

impl<T: Eq> Eq for Wrapping<T>

1.19.0 · source§

impl<T: Eq> Eq for Reverse<T>

1.0.0 · source§

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

1.0.0 · source§

impl<T: Eq, const N: usize> Eq for [T; N]

1.2.0 · source§

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

1.0.0 · source§

impl<T: ?Sized> Eq for *const T

1.0.0 · source§

impl<T: ?Sized> Eq for *mut T

1.0.0 · source§

impl<T: ?Sized> Eq for PhantomData<T>

1.25.0 · source§

impl<T: ?Sized> Eq for NonNull<T>

source§

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