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
impliesb == a
anda != b
implies!(a == b)
- transitive:
a == b
andb == c
impliesa == 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 derive
d, 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:
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
impl Eq for AsciiChar
impl Eq for Infallible
impl Eq for core::fmt::Alignment
impl Eq for IpAddr
impl Eq for Ipv6MulticastScope
impl Eq for SocketAddr
impl Eq for FpCategory
impl Eq for IntErrorKind
impl Eq for SearchStep
impl Eq for core::sync::atomic::Ordering
impl Eq for core::cmp::Ordering
impl Eq for bool
impl Eq for char
impl Eq for i8
impl Eq for i16
impl Eq for i32
impl Eq for i64
impl Eq for i128
impl Eq for isize
impl Eq for !
impl Eq for str
impl Eq for u8
impl Eq for u16
impl Eq for u32
impl Eq for u64
impl Eq for u128
impl Eq for ()
impl Eq for usize
impl Eq for AllocError
impl Eq for Layout
impl Eq for LayoutError
impl Eq for TypeId
impl Eq for CpuidResult
impl Eq for CharTryFromError
impl Eq for DecodeUtf16Error
impl Eq for ParseCharError
impl Eq for TryFromCharError
impl Eq for FromBytesUntilNulError
impl Eq for FromBytesWithNulError
impl Eq for CStr
impl Eq for Error
impl Eq for PhantomPinned
impl Eq for Assume
impl Eq for AddrParseError
impl Eq for Ipv4Addr
impl Eq for Ipv6Addr
impl Eq for SocketAddrV4
impl Eq for SocketAddrV6
impl Eq for ParseFloatError
impl Eq for ParseIntError
impl Eq for TryFromIntError
impl Eq for RangeFull
impl Eq for core::ptr::Alignment
impl Eq for ParseBoolError
impl Eq for Utf8Error
impl Eq for Duration
impl Eq for TryFromFloatSecsError
impl<'a> Eq for Utf8Pattern<'a>
impl<'a> Eq for Location<'a>
impl<'a> Eq for Utf8Chunk<'a>
impl<A> Eq for &A
impl<A> Eq for &mut A
impl<B: Eq, C: Eq> Eq for ControlFlow<B, C>
impl<Dyn: ?Sized> Eq for DynMetadata<Dyn>
impl<F: FnPtr> Eq for F
impl<H> Eq for BuildHasherDefault<H>
impl<Idx: Eq> Eq for core::ops::Range<Idx>
impl<Idx: Eq> Eq for core::ops::RangeFrom<Idx>
impl<Idx: Eq> Eq for core::ops::RangeInclusive<Idx>
impl<Idx: Eq> Eq for RangeTo<Idx>
impl<Idx: Eq> Eq for RangeToInclusive<Idx>
impl<Idx: Eq> Eq for core::range::Range<Idx>
impl<Idx: Eq> Eq for core::range::RangeFrom<Idx>
impl<Idx: Eq> Eq for core::range::RangeInclusive<Idx>
impl<Ptr: Deref<Target: Eq>> Eq for Pin<Ptr>
impl<T> Eq for (T₁, T₂, …, Tₙ)
This trait is implemented for tuples up to twelve items long.