pub trait Eq: PartialEq<Self> {
// Provided method
fn assert_receiver_is_total_eq(&self) { ... }
}
Expand description
Trait for equality comparisons which are equivalence relations.
This means, that in addition to a == b
and a != b
being strict inverses, the equality must
be (for all a
, b
and c
):
- reflexive:
a == a
; - symmetric:
a == b
impliesb == a
; and - transitive:
a == b
andb == c
impliesa == c
.
This property cannot be checked by the compiler, and therefore Eq
implies
PartialEq
, and has no extra methods.
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 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 {}
RunProvided Methods§
fn assert_receiver_is_total_eq(&self)
Implementors§
impl Eq for hashbrown::TryReserveError
impl Eq for AsciiChar
impl Eq for BacktraceStatus
impl Eq for PrintFmt
impl Eq for TryReserveErrorKind
impl Eq for Infallible
impl Eq for VarError
impl Eq for std::fmt::Alignment
impl Eq for SeekFrom
impl Eq for ErrorKind
impl Eq for IpAddr
impl Eq for Ipv6MulticastScope
impl Eq for Shutdown
impl Eq for SocketAddr
impl Eq for FpCategory
impl Eq for IntErrorKind
impl Eq for BacktraceStyle
impl Eq for Which
impl Eq for SearchStep
impl Eq for std::sync::atomic::Ordering
impl Eq for Selected
impl Eq for RecvTimeoutError
impl Eq for TryRecvError
impl Eq for ReparsePoint
impl Eq for std::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 CpuidResult
impl Eq for FromBytesUntilNulError
impl Eq for AllocError
impl Eq for Layout
impl Eq for LayoutError
impl Eq for TypeId
impl Eq for CharTryFromError
impl Eq for DecodeUtf16Error
impl Eq for ParseCharError
impl Eq for TryFromCharError
impl Eq for std::collections::TryReserveError
impl Eq for OsStr
impl Eq for OsString
impl Eq for CStr
impl Eq for CString
impl Eq for FromBytesWithNulError
impl Eq for FromVecWithNulError
impl Eq for IntoStringError
impl Eq for NulError
impl Eq for Error
impl Eq for std::fs::FileType
impl Eq for Permissions
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 NonZeroI8
impl Eq for NonZeroI16
impl Eq for NonZeroI32
impl Eq for NonZeroI64
impl Eq for NonZeroI128
impl Eq for NonZeroIsize
impl Eq for NonZeroU8
impl Eq for NonZeroU16
impl Eq for NonZeroU32
impl Eq for NonZeroU64
impl Eq for NonZeroU128
impl Eq for NonZeroUsize
impl Eq for ParseFloatError
impl Eq for ParseIntError
impl Eq for TryFromIntError
impl Eq for RangeFull
impl Eq for InvalidHandleError
Available on Windows only.
impl Eq for NullHandleError
Available on Windows only.
impl Eq for Components<'_>
impl Eq for Path
impl Eq for PathBuf
impl Eq for StripPrefixError
impl Eq for std::process::ExitStatus
impl Eq for std::process::ExitStatusError
impl Eq for Output
impl Eq for std::ptr::Alignment
impl Eq for ParseBoolError
impl Eq for Utf8Error
impl Eq for FromUtf8Error
impl Eq for String
impl Eq for WaitTimeoutResult
impl Eq for Operation
impl Eq for RecvError
impl Eq for FilePermissions
impl Eq for std::sys::windows::fs::FileType
impl Eq for ExitCode
impl Eq for std::sys::windows::process::ExitStatus
impl Eq for std::sys::windows::process::ExitStatusError
impl Eq for std::sys::windows::time::Instant
impl Eq for std::sys::windows::time::SystemTime
impl Eq for CodePoint
impl Eq for Wtf8
impl Eq for Wtf8Buf
impl Eq for AccessError
impl Eq for ThreadId
impl Eq for Duration
impl Eq for std::time::Instant
impl Eq for std::time::SystemTime
impl Eq for TryFromFloatSecsError
impl<'a> Eq for Component<'a>
impl<'a> Eq for Prefix<'a>
impl<'a> Eq for Location<'a>
impl<'a> Eq for PrefixComponent<'a>
impl<'a> Eq for Utf8Chunk<'a>
impl<A> Eq for &Awhere A: Eq + ?Sized,
impl<A> Eq for &mut Awhere A: Eq + ?Sized,
impl<A, Z, Y, X, W, V, U, T> Eq for (A, Z, Y, X, W, V, U, T)where A: Eq, Z: Eq, Y: Eq, X: Eq, W: Eq, V: Eq, U: Eq, T: Eq + ?Sized,
impl<B> Eq for Cow<'_, B>where B: Eq + ToOwned + ?Sized,
impl<B, A, Z, Y, X, W, V, U, T> Eq for (B, A, Z, Y, X, W, V, U, T)where B: Eq, A: Eq, Z: Eq, Y: Eq, X: Eq, W: Eq, V: Eq, U: Eq, T: Eq + ?Sized,
impl<B, C> Eq for ControlFlow<B, C>where B: Eq, C: Eq,
impl<C, B, A, Z, Y, X, W, V, U, T> Eq for (C, B, A, Z, Y, X, W, V, U, T)where C: Eq, B: Eq, A: Eq, Z: Eq, Y: Eq, X: Eq, W: Eq, V: Eq, U: Eq, T: Eq + ?Sized,
impl<D, C, B, A, Z, Y, X, W, V, U, T> Eq for (D, C, B, A, Z, Y, X, W, V, U, T)where D: Eq, C: Eq, B: Eq, A: Eq, Z: Eq, Y: Eq, X: Eq, W: Eq, V: Eq, U: Eq, T: Eq + ?Sized,
impl<Dyn> Eq for DynMetadata<Dyn>where Dyn: ?Sized,
impl<E, D, C, B, A, Z, Y, X, W, V, U, T> Eq for (E, D, C, B, A, Z, Y, X, W, V, U, T)where E: Eq, D: Eq, C: Eq, B: Eq, A: Eq, Z: Eq, Y: Eq, X: Eq, W: Eq, V: Eq, U: Eq, T: Eq + ?Sized,
impl<F> Eq for Fwhere F: FnPtr,
impl<H> Eq for BuildHasherDefault<H>
impl<Idx> Eq for Range<Idx>where Idx: Eq,
impl<Idx> Eq for RangeFrom<Idx>where Idx: Eq,
impl<Idx> Eq for RangeInclusive<Idx>where Idx: Eq,
impl<Idx> Eq for RangeTo<Idx>where Idx: Eq,
impl<Idx> Eq for RangeToInclusive<Idx>where Idx: Eq,
impl<K, V, A> Eq for BTreeMap<K, V, A>where K: Eq, V: Eq, A: Allocator + Clone,
impl<K, V, S> Eq for std::collections::hash::map::HashMap<K, V, S>where K: Eq + Hash, V: Eq, S: BuildHasher,
impl<K, V, S, A> Eq for hashbrown::map::HashMap<K, V, S, A>where K: Eq + Hash, V: Eq, S: BuildHasher, A: Allocator + Clone,
impl<P> Eq for Pin<P>where P: Deref, <P as Deref>::Target: Eq,
impl<T> Eq for Bound<T>where T: Eq,
impl<T> Eq for Option<T>where T: Eq,
impl<T> Eq for Poll<T>where T: Eq,
impl<T> Eq for *const Twhere T: ?Sized,
impl<T> Eq for *mut Twhere T: ?Sized,
impl<T> Eq for [T]where T: Eq,
impl<T> Eq for (T₁, T₂, …, Tₙ)where T: Eq + ?Sized,
This trait is implemented for tuples up to twelve items long.