pub trait PartialEq<Rhs = Self>where Rhs: ?Sized,{ // Required method fneq(&self, other:&Rhs) ->bool; // Provided method fnne(&self, other:&Rhs) ->bool { ... }}Expand description
Trait for comparisons using the equality operator.
Implementing this trait for types provides the== and!= operators forthose types.
x.eq(y) can also be writtenx == y, andx.ne(y) can be writtenx != 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 typesthat do not have a full equivalence relation. For example, in floating pointnumbersNaN != NaN, so floating point types implementPartialEq but notEq. Formally speaking, whenRhs == Self, this trait correspondsto apartial equivalence relation.
Implementations must ensure thateq andne are consistent with each other:
a != bif and only if!(a == b).
The default implementation ofne provides this consistency and is almostalways sufficient. It should not be overridden without very good reason.
IfPartialOrd orOrd are also implemented forSelf andRhs, their methods must alsobe consistent withPartialEq (see the documentation of those traits for the exactrequirements). It’s easy to accidentally make them disagree by deriving some of the traits andmanually implementing others.
The equality relation== must satisfy the following conditions(for alla,b,c of typeA,B,C):
Symmetry: if
A: PartialEq<B>andB: PartialEq<A>, thena == bimpliesb == a; andTransitivity: if
A: PartialEq<B>andB: PartialEq<C>andA: PartialEq<C>, thena == bandb == cimpliesa == c.This must also work for longer chains, such as whenA: PartialEq<B>,B: PartialEq<C>,C: PartialEq<D>, andA: PartialEq<D>all exist.
Note that theB: PartialEq<A> (symmetric) andA: PartialEq<C>(transitive) impls are not forced to exist, but these requirements applywhenever they do exist.
Violating these requirements is a logic error. The behavior resulting from a logic error is notspecified, but users of the trait must ensure that such logic errors donot result inundefined behavior. This means thatunsafe codemust not rely on the correctness of thesemethods.
§Cross-crate considerations
Upholding the requirements stated above can become tricky when one crate implementsPartialEqfor a type of another crate (i.e., to allow comparing one of its own types with a type from thestandard library). The recommendation is to never implement this trait for a foreign type. Inother words, such a crate should doimpl PartialEq<ForeignType> for LocalType, but it shouldnot doimpl PartialEq<LocalType> for ForeignType.
This avoids the problem of transitive chains that criss-cross crate boundaries: for all localtypesT, you may assume that no other crate will addimpls that allow comparingT == U. Inother words, if other crates addimpls that allow building longer transitive chainsU1 == ... == T == V1 == ..., then all the types that appear to the right ofT must be types that thecrate definingT already knows about. This rules out transitive chains where downstream cratescan add newimpls that “stitch together” comparisons of foreign types in ways that violatetransitivity.
Not having such foreignimpls also avoids forward compatibility issues where one crate addingmorePartialEq implementations can cause build failures in downstream crates.
§Derivable
This trait can be used with#[derive]. Whenderived on structs, twoinstances are equal if all fields are equal, and not equal if any fieldsare not equal. Whenderived on enums, two instances are equal if theyare the same variant and all fields are equal.
§How can I implementPartialEq?
An example implementation for a domain in which two books are consideredthe same book if their ISBN matches, even if the formats differ:
enumBookFormat { Paperback, Hardback, Ebook,}structBook { isbn: i32, format: BookFormat,}implPartialEqforBook {fneq(&self, other:&Self) -> bool {self.isbn == other.isbn }}letb1 = Book { isbn:3, format: BookFormat::Paperback };letb2 = Book { isbn:3, format: BookFormat::Ebook };letb3 = 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 byPartialEq’s type parameter.For example, let’s tweak our previous code a bit:
// The derive implements <BookFormat> == <BookFormat> comparisons#[derive(PartialEq)]enumBookFormat { Paperback, Hardback, Ebook,}structBook { isbn: i32, format: BookFormat,}// Implement <Book> == <BookFormat> comparisonsimplPartialEq<BookFormat>forBook {fneq(&self, other:&BookFormat) -> bool {self.format ==*other }}// Implement <BookFormat> == <Book> comparisonsimplPartialEq<Book>forBookFormat {fneq(&self, other:&Book) -> bool {*self== other.format }}letb1 = Book { isbn:3, format: BookFormat::Paperback };assert!(b1 == BookFormat::Paperback);assert!(BookFormat::Ebook != b1);By changingimpl PartialEq for Book toimpl PartialEq<BookFormat> for Book,we allowBookFormats to be compared withBooks.
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 therequirements for a partial equivalence relation. For example, if we keptthe above implementation ofPartialEq<Book> forBookFormat and added animplementation ofPartialEq<Book> forBook (either via a#[derive] orvia the manual implementation from the first example) then the result wouldviolate transitivity:
#[derive(PartialEq)]enumBookFormat { Paperback, Hardback, Ebook,}#[derive(PartialEq)]structBook { isbn: i32, format: BookFormat,}implPartialEq<BookFormat>forBook {fneq(&self, other:&BookFormat) -> bool {self.format ==*other }}implPartialEq<Book>forBookFormat {fneq(&self, other:&Book) -> bool {*self== other.format }}fnmain() {letb1 = Book { isbn:1, format: BookFormat::Paperback };letb2 = 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
Required Methods§
Provided Methods§
Implementors§
implPartialEq forAsciiChar
implPartialEq forBacktraceStatus
implPartialEq forTryReserveErrorKind
implPartialEq forInfallible
implPartialEq forVarError
implPartialEq forFromBytesWithNulError
implPartialEq for std::fmt::Alignment
implPartialEq forDebugAsHex
implPartialEq forSign
implPartialEq forAtomicOrdering
implPartialEq forSimdAlign
implPartialEq forErrorKind
implPartialEq forSeekFrom
implPartialEq forIpAddr
implPartialEq forIpv6MulticastScope
implPartialEq forShutdown
implPartialEq forSocketAddr
implPartialEq forFpCategory
implPartialEq forIntErrorKind
implPartialEq forBacktraceStyle
implPartialEq forGetDisjointMutError
implPartialEq forSearchStep
implPartialEq for std::sync::atomic::Ordering
implPartialEq forRecvTimeoutError
implPartialEq forTryRecvError
implPartialEq for std::cmp::Ordering
implPartialEq forbool
implPartialEq forchar
implPartialEq forf16
implPartialEq forf32
implPartialEq forf64
implPartialEq forf128
implPartialEq fori8
implPartialEq fori16
implPartialEq fori32
implPartialEq fori64
implPartialEq fori128
implPartialEq forisize
implPartialEq for!
implPartialEq forstr
implPartialEq foru8
implPartialEq foru16
implPartialEq foru32
implPartialEq foru64
implPartialEq foru128
implPartialEq for()
implPartialEq forusize
implPartialEq forCpuidResult
implPartialEq forAllocError
implPartialEq forLayout
implPartialEq forLayoutError
implPartialEq forTypeId
implPartialEq forByteStr
implPartialEq forByteString
implPartialEq forCharTryFromError
implPartialEq forDecodeUtf16Error
implPartialEq forParseCharError
implPartialEq forTryFromCharError
implPartialEq forUnorderedKeyError
implPartialEq forTryReserveError
implPartialEq forCStr
implPartialEq forCString
implPartialEq forFromBytesUntilNulError
implPartialEq forFromVecWithNulError
implPartialEq forIntoStringError
implPartialEq forNulError
implPartialEq forOsStr
implPartialEq forOsString
implPartialEq forError
implPartialEq forFormattingOptions
implPartialEq forFileType
implPartialEq forPermissions
implPartialEq forPhantomPinned
implPartialEq forAssume
implPartialEq forAddrParseError
implPartialEq forIpv4Addr
implPartialEq forIpv6Addr
implPartialEq forSocketAddrV4
implPartialEq forSocketAddrV6
implPartialEq forParseFloatError
implPartialEq forParseIntError
implPartialEq forTryFromIntError
implPartialEq forRangeFull
implPartialEq forUCred
implPartialEq forInvalidHandleError
implPartialEq forNullHandleError
implPartialEq forLocation<'_>
implPartialEq forNormalizeError
implPartialEq forPath
implPartialEq forPathBuf
implPartialEq forStripPrefixError
implPartialEq forExitCode
implPartialEq forExitStatus
implPartialEq forExitStatusError
implPartialEq forOutput
implPartialEq for std::ptr::Alignment
implPartialEq forParseBoolError
implPartialEq forUtf8Error
implPartialEq forFromUtf8Error
implPartialEq forString
implPartialEq forRecvError
implPartialEq forWaitTimeoutResult
implPartialEq forRawWaker
implPartialEq forRawWakerVTable
implPartialEq forAccessError
implPartialEq forThreadId
implPartialEq forDuration
implPartialEq forInstant
implPartialEq forSystemTime
implPartialEq forTryFromFloatSecsError
implPartialEq<&str> forOsString
implPartialEq<&CStr> forCow<'_,CStr>
implPartialEq<&CStr> forCStr
implPartialEq<&CStr> forCString
implPartialEq<Cow<'_,CStr>> forCStr
implPartialEq<Cow<'_,CStr>> forCString
implPartialEq<IpAddr> forIpv4Addr
implPartialEq<IpAddr> forIpv6Addr
implPartialEq<str> forOsStr
implPartialEq<str> forOsString
implPartialEq<str> forPath
implPartialEq<str> forPathBuf
implPartialEq<CStr> forCow<'_,CStr>
implPartialEq<CStr> forCString
implPartialEq<CString> forCow<'_,CStr>
implPartialEq<CString> forCStr
implPartialEq<OsStr> forstr
implPartialEq<OsStr> forPath
implPartialEq<OsStr> forPathBuf
implPartialEq<OsString> forstr
implPartialEq<OsString> forPath
implPartialEq<OsString> forPathBuf
implPartialEq<Ipv4Addr> forIpAddr
implPartialEq<Ipv6Addr> forIpAddr
implPartialEq<Path> forstr
implPartialEq<Path> forOsStr
implPartialEq<Path> forOsString
implPartialEq<Path> forPathBuf
implPartialEq<Path> forString
implPartialEq<PathBuf> forstr
implPartialEq<PathBuf> forOsStr
implPartialEq<PathBuf> forOsString
implPartialEq<PathBuf> forPath
implPartialEq<PathBuf> forString
implPartialEq<String> forPath
implPartialEq<String> forPathBuf
impl<'a>PartialEq forComponent<'a>
impl<'a>PartialEq forPrefix<'a>
impl<'a>PartialEq forUtf8Pattern<'a>
impl<'a>PartialEq forPhantomContravariantLifetime<'a>
impl<'a>PartialEq forPhantomCovariantLifetime<'a>
impl<'a>PartialEq forPhantomInvariantLifetime<'a>
impl<'a>PartialEq forComponents<'a>
impl<'a>PartialEq forPrefixComponent<'a>
impl<'a>PartialEq forUtf8Chunk<'a>
impl<'a>PartialEq<&'aByteStr> forCow<'a,str>
impl<'a>PartialEq<&'aByteStr> forCow<'a,ByteStr>
impl<'a>PartialEq<&'aByteStr> forCow<'a, [u8]>
impl<'a>PartialEq<&'aOsStr> forPath
impl<'a>PartialEq<&'aOsStr> forPathBuf
impl<'a>PartialEq<&'aPath> forOsStr
impl<'a>PartialEq<&'aPath> forOsString
impl<'a>PartialEq<&'aPath> forPathBuf
impl<'a>PartialEq<&str> forByteStr
impl<'a>PartialEq<&str> forByteString
impl<'a>PartialEq<&ByteStr> forByteString
impl<'a>PartialEq<&[u8]> forByteStr
impl<'a>PartialEq<&[u8]> forByteString
impl<'a>PartialEq<Cow<'_,str>> forByteString
impl<'a>PartialEq<Cow<'_,ByteStr>> forByteString
impl<'a>PartialEq<Cow<'_, [u8]>> forByteString
impl<'a>PartialEq<Cow<'a,str>> for &'aByteStr
impl<'a>PartialEq<Cow<'a,ByteStr>> for &'aByteStr
impl<'a>PartialEq<Cow<'a,OsStr>> forPath
impl<'a>PartialEq<Cow<'a,OsStr>> forPathBuf
impl<'a>PartialEq<Cow<'a,Path>> forOsStr
impl<'a>PartialEq<Cow<'a,Path>> forOsString
impl<'a>PartialEq<Cow<'a,Path>> forPath
impl<'a>PartialEq<Cow<'a,Path>> forPathBuf
impl<'a>PartialEq<Cow<'a, [u8]>> for &'aByteStr
impl<'a>PartialEq<str> forByteStr
impl<'a>PartialEq<str> forByteString
impl<'a>PartialEq<ByteStr> for &str
impl<'a>PartialEq<ByteStr> for &[u8]
impl<'a>PartialEq<ByteStr> forstr
impl<'a>PartialEq<ByteStr> forByteString
impl<'a>PartialEq<ByteStr> forString
impl<'a>PartialEq<ByteStr> forVec<u8>
impl<'a>PartialEq<ByteStr> for [u8]
impl<'a>PartialEq<ByteString> for &str
impl<'a>PartialEq<ByteString> for &ByteStr
impl<'a>PartialEq<ByteString> for &[u8]
impl<'a>PartialEq<ByteString> forCow<'_,str>
impl<'a>PartialEq<ByteString> forCow<'_,ByteStr>
impl<'a>PartialEq<ByteString> forCow<'_, [u8]>
impl<'a>PartialEq<ByteString> forstr
impl<'a>PartialEq<ByteString> forByteStr
impl<'a>PartialEq<ByteString> forString
impl<'a>PartialEq<ByteString> forVec<u8>
impl<'a>PartialEq<ByteString> for [u8]
impl<'a>PartialEq<OsStr> for &'aPath
impl<'a>PartialEq<OsStr> forCow<'a,Path>
impl<'a>PartialEq<OsString> for &'astr
impl<'a>PartialEq<OsString> for &'aPath
impl<'a>PartialEq<OsString> forCow<'a,Path>
impl<'a>PartialEq<Path> for &'aOsStr
impl<'a>PartialEq<Path> forCow<'a,OsStr>
impl<'a>PartialEq<Path> forCow<'a,Path>
impl<'a>PartialEq<PathBuf> for &'aOsStr
impl<'a>PartialEq<PathBuf> for &'aPath
impl<'a>PartialEq<PathBuf> forCow<'a,OsStr>
impl<'a>PartialEq<PathBuf> forCow<'a,Path>
impl<'a>PartialEq<String> forByteStr
impl<'a>PartialEq<String> forByteString
impl<'a>PartialEq<Vec<u8>> forByteStr
impl<'a>PartialEq<Vec<u8>> forByteString
impl<'a>PartialEq<[u8]> forByteStr
impl<'a>PartialEq<[u8]> forByteString
impl<'a, 'b>PartialEq<&'astr> forString
impl<'a, 'b>PartialEq<&'aOsStr> forOsString
impl<'a, 'b>PartialEq<&'aPath> forCow<'b,OsStr>
impl<'a, 'b>PartialEq<&'bstr> forCow<'a,str>
impl<'a, 'b>PartialEq<&'bOsStr> forCow<'a,OsStr>
impl<'a, 'b>PartialEq<&'bOsStr> forCow<'a,Path>
impl<'a, 'b>PartialEq<&'bPath> forCow<'a,Path>
impl<'a, 'b>PartialEq<Cow<'a,str>> for &'bstr
impl<'a, 'b>PartialEq<Cow<'a,str>> forstr
impl<'a, 'b>PartialEq<Cow<'a,str>> forString
impl<'a, 'b>PartialEq<Cow<'a,OsStr>> for &'bOsStr
impl<'a, 'b>PartialEq<Cow<'a,OsStr>> forOsStr
impl<'a, 'b>PartialEq<Cow<'a,OsStr>> forOsString
impl<'a, 'b>PartialEq<Cow<'a,Path>> for &'bOsStr
impl<'a, 'b>PartialEq<Cow<'a,Path>> for &'bPath
impl<'a, 'b>PartialEq<Cow<'b,OsStr>> for &'aPath
impl<'a, 'b>PartialEq<str> forCow<'a,str>
impl<'a, 'b>PartialEq<str> forString
impl<'a, 'b>PartialEq<OsStr> forCow<'a,OsStr>
impl<'a, 'b>PartialEq<OsStr> forOsString
impl<'a, 'b>PartialEq<OsString> for &'aOsStr
impl<'a, 'b>PartialEq<OsString> forCow<'a,OsStr>
impl<'a, 'b>PartialEq<OsString> forOsStr
impl<'a, 'b>PartialEq<String> for &'astr
impl<'a, 'b>PartialEq<String> forCow<'a,str>
impl<'a, 'b>PartialEq<String> forstr
impl<'a, 'b, B, C>PartialEq<Cow<'b, C>> forCow<'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<B, C>PartialEq forControlFlow<B, C>
impl<Dyn>PartialEq forDynMetadata<Dyn>where Dyn: ?Sized,
impl<F>PartialEq for Fwhere F:FnPtr,
impl<H>PartialEq forBuildHasherDefault<H>
impl<Idx>PartialEq for std::ops::Range<Idx>where Idx:PartialEq,
impl<Idx>PartialEq for std::ops::RangeFrom<Idx>where Idx:PartialEq,
impl<Idx>PartialEq for std::ops::RangeInclusive<Idx>where Idx:PartialEq,
impl<Idx>PartialEq forRangeTo<Idx>where Idx:PartialEq,
impl<Idx>PartialEq for std::ops::RangeToInclusive<Idx>where Idx:PartialEq,
impl<Idx>PartialEq for std::range::Range<Idx>where Idx:PartialEq,
impl<Idx>PartialEq for std::range::RangeFrom<Idx>where Idx:PartialEq,
impl<Idx>PartialEq for std::range::RangeInclusive<Idx>where Idx:PartialEq,
impl<Idx>PartialEq for std::range::RangeToInclusive<Idx>where Idx:PartialEq,
impl<K, V, A>PartialEq forBTreeMap<K, V, A>
impl<K, V, S>PartialEq forHashMap<K, V, S>
impl<Ptr, Q>PartialEq<Pin<Q>> forPin<Ptr>
impl<T>PartialEq forBound<T>where T:PartialEq,
impl<T>PartialEq forOption<T>where T:PartialEq,
impl<T>PartialEq forPoll<T>where T:PartialEq,
impl<T>PartialEq for*const Twhere T: ?Sized,
Pointer equality is by address, as produced by the<*const T>::addr method.
impl<T>PartialEq for*mut Twhere T: ?Sized,
Pointer equality is by address, as produced by the<*mut T>::addr method.
impl<T>PartialEq for(T₁, T₂, …, Tₙ)where T:PartialEq,
This trait is implemented for tuples up to twelve items long.