pub trait PartialEq<Rhs = Self>where
Rhs: ?Sized,{
// 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
):
-
Symmetric: if
A: PartialEq<B>
andB: PartialEq<A>
, thena == b
impliesb == a
; and -
Transitive: if
A: PartialEq<B>
andB: PartialEq<C>
andA: PartialEq<C>
, thena == b
andb == c
impliesa == c
.
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.
Derivable
This trait can be used with #[derive]
. When derive
d on structs, two
instances are equal if all fields are equal, and not equal if any fields
are not equal. When derive
d 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);
RunHow 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);
RunBy changing impl PartialEq for Book
to impl PartialEq<BookFormat> for Book
,
we allow BookFormat
s to be compared with Book
s.
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
}
RunExamples
let x: u32 = 0;
let y: u32 = 1;
assert_eq!(x == y, false);
assert_eq!(x.eq(&y), false);
RunRequired Methods§
Provided Methods§
Implementors§
impl PartialEq for AsciiChar
impl PartialEq for BacktraceStatus
impl PartialEq for TryReserveErrorKind
impl PartialEq for Infallible
impl PartialEq for VarError
impl PartialEq for std::fmt::Alignment
impl PartialEq for ErrorKind
impl PartialEq for SeekFrom
impl PartialEq for IpAddr
impl PartialEq for Ipv6MulticastScope
impl PartialEq for Shutdown
impl PartialEq for SocketAddr
impl PartialEq for FpCategory
impl PartialEq for IntErrorKind
impl PartialEq for BacktraceStyle
impl PartialEq for SearchStep
impl PartialEq for std::sync::atomic::Ordering
impl PartialEq for RecvTimeoutError
impl PartialEq for TryRecvError
impl PartialEq for std::cmp::Ordering
impl PartialEq for bool
impl PartialEq for char
impl PartialEq for f32
impl PartialEq for f64
impl PartialEq for i8
impl PartialEq for i16
impl PartialEq for i32
impl PartialEq for i64
impl PartialEq for i128
impl PartialEq for isize
impl PartialEq for !
impl PartialEq for str
impl PartialEq for u8
impl PartialEq for u16
impl PartialEq for u32
impl PartialEq for u64
impl PartialEq for u128
impl PartialEq for ()
impl PartialEq for usize
impl PartialEq for CpuidResult
impl PartialEq for AllocError
impl PartialEq for Layout
impl PartialEq for LayoutError
impl PartialEq for TypeId
impl PartialEq for CharTryFromError
impl PartialEq for DecodeUtf16Error
impl PartialEq for ParseCharError
impl PartialEq for TryFromCharError
impl PartialEq for TryReserveError
impl PartialEq for CStr
impl PartialEq for CString
impl PartialEq for FromBytesUntilNulError
impl PartialEq for FromBytesWithNulError
impl PartialEq for FromVecWithNulError
impl PartialEq for IntoStringError
impl PartialEq for NulError
impl PartialEq for OsStr
impl PartialEq for OsString
impl PartialEq for Error
impl PartialEq for FileType
impl PartialEq for Permissions
impl PartialEq for PhantomPinned
impl PartialEq for Assume
impl PartialEq for AddrParseError
impl PartialEq for Ipv4Addr
impl PartialEq for Ipv6Addr
impl PartialEq for SocketAddrV4
impl PartialEq for SocketAddrV6
impl PartialEq for NonZeroI8
impl PartialEq for NonZeroI16
impl PartialEq for NonZeroI32
impl PartialEq for NonZeroI64
impl PartialEq for NonZeroI128
impl PartialEq for NonZeroIsize
impl PartialEq for NonZeroU8
impl PartialEq for NonZeroU16
impl PartialEq for NonZeroU32
impl PartialEq for NonZeroU64
impl PartialEq for NonZeroU128
impl PartialEq for NonZeroUsize
impl PartialEq for ParseFloatError
impl PartialEq for ParseIntError
impl PartialEq for TryFromIntError
impl PartialEq for RangeFull
impl PartialEq for UCred
impl PartialEq for InvalidHandleError
impl PartialEq for NullHandleError
impl PartialEq for Path
impl PartialEq for PathBuf
impl PartialEq for StripPrefixError
impl PartialEq for ExitStatus
impl PartialEq for ExitStatusError
impl PartialEq for Output
impl PartialEq for std::ptr::Alignment
impl PartialEq for ParseBoolError
impl PartialEq for Utf8Error
impl PartialEq for FromUtf8Error
impl PartialEq for String
impl PartialEq for RecvError
impl PartialEq for WaitTimeoutResult
impl PartialEq for RawWaker
impl PartialEq for RawWakerVTable
impl PartialEq for AccessError
impl PartialEq for ThreadId
impl PartialEq for Duration
impl PartialEq for Instant
impl PartialEq for SystemTime
impl PartialEq for TryFromFloatSecsError
impl PartialEq<&str> for OsString
impl PartialEq<IpAddr> for Ipv4Addr
impl PartialEq<IpAddr> for Ipv6Addr
impl PartialEq<str> for OsStr
impl PartialEq<str> for OsString
impl PartialEq<OsStr> for str
impl PartialEq<OsStr> for Path
impl PartialEq<OsStr> for PathBuf
impl PartialEq<OsString> for str
impl PartialEq<OsString> for Path
impl PartialEq<OsString> for PathBuf
impl PartialEq<Ipv4Addr> for IpAddr
impl PartialEq<Ipv6Addr> for IpAddr
impl PartialEq<Path> for OsStr
impl PartialEq<Path> for OsString
impl PartialEq<Path> for PathBuf
impl PartialEq<PathBuf> for OsStr
impl PartialEq<PathBuf> for OsString
impl PartialEq<PathBuf> for Path
impl<'a> PartialEq for Component<'a>
impl<'a> PartialEq for Prefix<'a>
impl<'a> PartialEq for Location<'a>
impl<'a> PartialEq for Components<'a>
impl<'a> PartialEq for PrefixComponent<'a>
impl<'a> PartialEq for Utf8Chunk<'a>
impl<'a> PartialEq<&'a OsStr> for Path
impl<'a> PartialEq<&'a OsStr> for PathBuf
impl<'a> PartialEq<&'a Path> for OsStr
impl<'a> PartialEq<&'a Path> for OsString
impl<'a> PartialEq<&'a Path> for PathBuf
impl<'a> PartialEq<Cow<'a, OsStr>> for Path
impl<'a> PartialEq<Cow<'a, OsStr>> for PathBuf
impl<'a> PartialEq<Cow<'a, Path>> for OsStr
impl<'a> PartialEq<Cow<'a, Path>> for OsString
impl<'a> PartialEq<Cow<'a, Path>> for Path
impl<'a> PartialEq<Cow<'a, Path>> for PathBuf
impl<'a> PartialEq<OsStr> for &'a Path
impl<'a> PartialEq<OsStr> for Cow<'a, Path>
impl<'a> PartialEq<OsString> for &'a str
impl<'a> PartialEq<OsString> for &'a Path
impl<'a> PartialEq<OsString> for Cow<'a, Path>
impl<'a> PartialEq<Path> for &'a OsStr
impl<'a> PartialEq<Path> for Cow<'a, OsStr>
impl<'a> PartialEq<Path> for Cow<'a, Path>
impl<'a> PartialEq<PathBuf> for &'a OsStr
impl<'a> PartialEq<PathBuf> for &'a Path
impl<'a> PartialEq<PathBuf> for Cow<'a, OsStr>
impl<'a> PartialEq<PathBuf> for Cow<'a, Path>
impl<'a, 'b> PartialEq<&'a str> for String
impl<'a, 'b> PartialEq<&'a OsStr> for OsString
impl<'a, 'b> PartialEq<&'a Path> for Cow<'b, OsStr>
impl<'a, 'b> PartialEq<&'b str> for Cow<'a, str>
impl<'a, 'b> PartialEq<&'b OsStr> for Cow<'a, OsStr>
impl<'a, 'b> PartialEq<&'b OsStr> for Cow<'a, Path>
impl<'a, 'b> PartialEq<&'b Path> for Cow<'a, Path>
impl<'a, 'b> PartialEq<Cow<'a, str>> for &'b str
impl<'a, 'b> PartialEq<Cow<'a, str>> for str
impl<'a, 'b> PartialEq<Cow<'a, str>> for String
impl<'a, 'b> PartialEq<Cow<'a, OsStr>> for &'b OsStr
impl<'a, 'b> PartialEq<Cow<'a, OsStr>> for OsStr
impl<'a, 'b> PartialEq<Cow<'a, OsStr>> for OsString
impl<'a, 'b> PartialEq<Cow<'a, Path>> for &'b OsStr
impl<'a, 'b> PartialEq<Cow<'a, Path>> for &'b Path
impl<'a, 'b> PartialEq<Cow<'b, OsStr>> for &'a Path
impl<'a, 'b> PartialEq<str> for Cow<'a, str>
impl<'a, 'b> PartialEq<str> for String
impl<'a, 'b> PartialEq<OsStr> for Cow<'a, OsStr>
impl<'a, 'b> PartialEq<OsStr> for OsString
impl<'a, 'b> PartialEq<OsString> for &'a OsStr
impl<'a, 'b> PartialEq<OsString> for Cow<'a, OsStr>
impl<'a, 'b> PartialEq<OsString> for OsStr
impl<'a, 'b> PartialEq<String> for &'a str
impl<'a, 'b> PartialEq<String> for Cow<'a, str>
impl<'a, 'b> PartialEq<String> for str
impl<'a, 'b, B, C> PartialEq<Cow<'b, C>> for Cow<'a, B>
impl<A, B> PartialEq<&B> for &A
impl<A, B> PartialEq<&B> for &mut A
impl<A, B> PartialEq<&mut B> for &A
impl<A, B> PartialEq<&mut B> for &mut A
impl<A, B> PartialEq<[B]> for [A]where
A: PartialEq<B>,
impl<A, B, const N: usize> PartialEq<&[B]> for [A; N]where
A: PartialEq<B>,
impl<A, B, const N: usize> PartialEq<&mut [B]> for [A; N]where
A: PartialEq<B>,
impl<A, B, const N: usize> PartialEq<[A; N]> for &[B]where
B: PartialEq<A>,
impl<A, B, const N: usize> PartialEq<[A; N]> for &mut [B]where
B: PartialEq<A>,
impl<A, B, const N: usize> PartialEq<[A; N]> for [B]where
B: PartialEq<A>,
impl<A, B, const N: usize> PartialEq<[B; N]> for [A; N]where
A: PartialEq<B>,
impl<A, B, const N: usize> PartialEq<[B]> for [A; N]where
A: PartialEq<B>,
impl<B, C> PartialEq for ControlFlow<B, C>
impl<Dyn> PartialEq for DynMetadata<Dyn>where
Dyn: ?Sized,
impl<F> PartialEq for Fwhere
F: FnPtr,
impl<H> PartialEq for BuildHasherDefault<H>
impl<Idx> PartialEq for Range<Idx>where
Idx: PartialEq,
impl<Idx> PartialEq for RangeFrom<Idx>where
Idx: PartialEq,
impl<Idx> PartialEq for RangeInclusive<Idx>where
Idx: PartialEq,
impl<Idx> PartialEq for RangeTo<Idx>where
Idx: PartialEq,
impl<Idx> PartialEq for RangeToInclusive<Idx>where
Idx: PartialEq,
impl<K, V, A> PartialEq for BTreeMap<K, V, A>
impl<K, V, S> PartialEq for HashMap<K, V, S>
impl<P, Q> PartialEq<Pin<Q>> for Pin<P>
impl<T> PartialEq for Bound<T>where
T: PartialEq,
impl<T> PartialEq for Option<T>where
T: PartialEq,
impl<T> PartialEq for Poll<T>where
T: PartialEq,
impl<T> PartialEq for *const Twhere
T: ?Sized,
impl<T> PartialEq for *mut Twhere
T: ?Sized,
impl<T> PartialEq for (T₁, T₂, …, Tₙ)
This trait is implemented for tuples up to twelve items long.