Trait sp_std::prelude::PartialEq 1.0.0[−][src]
pub trait PartialEq<Rhs = Self> where
Rhs: ?Sized, { fn eq(&self, other: &Rhs) -> bool; fn ne(&self, other: &Rhs) -> bool { ... } }
Expand description
Trait for equality comparisons which are partial equivalence relations.
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 partial equality, 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
.
Implementations must ensure that eq
and ne
are consistent with each other:
a != b
if and only if!(a == b)
(ensured by the default implementation).
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.
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, each variant is equal to itself
and not equal to the other variants.
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);
How 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);
By 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 }
Examples
let x: u32 = 0; let y: u32 = 1; assert_eq!(x == y, false); assert_eq!(x.eq(&y), false);
Required methods
Provided methods
Implementations on Foreign Types
impl<A, B, C, D, E, F, G, H, I, J, K, L> PartialEq<(A, B, C, D, E, F, G, H, I, J, K, L)> for (A, B, C, D, E, F, G, H, I, J, K, L) where
C: PartialEq<C>,
A: PartialEq<A>,
E: PartialEq<E>,
K: PartialEq<K>,
F: PartialEq<F>,
I: PartialEq<I>,
B: PartialEq<B>,
H: PartialEq<H>,
D: PartialEq<D>,
G: PartialEq<G>,
J: PartialEq<J>,
L: PartialEq<L> + ?Sized,
impl<A, B, C, D, E, F, G, H, I, J, K, L> PartialEq<(A, B, C, D, E, F, G, H, I, J, K, L)> for (A, B, C, D, E, F, G, H, I, J, K, L) where
C: PartialEq<C>,
A: PartialEq<A>,
E: PartialEq<E>,
K: PartialEq<K>,
F: PartialEq<F>,
I: PartialEq<I>,
B: PartialEq<B>,
H: PartialEq<H>,
D: PartialEq<D>,
G: PartialEq<G>,
J: PartialEq<J>,
L: PartialEq<L> + ?Sized,
impl<A, B, C, D, E, F, G, H, I, J, K> PartialEq<(A, B, C, D, E, F, G, H, I, J, K)> for (A, B, C, D, E, F, G, H, I, J, K) where
C: PartialEq<C>,
A: PartialEq<A>,
E: PartialEq<E>,
K: PartialEq<K> + ?Sized,
F: PartialEq<F>,
I: PartialEq<I>,
B: PartialEq<B>,
H: PartialEq<H>,
D: PartialEq<D>,
G: PartialEq<G>,
J: PartialEq<J>,
impl<A, B, C, D, E, F, G, H, I, J, K> PartialEq<(A, B, C, D, E, F, G, H, I, J, K)> for (A, B, C, D, E, F, G, H, I, J, K) where
C: PartialEq<C>,
A: PartialEq<A>,
E: PartialEq<E>,
K: PartialEq<K> + ?Sized,
F: PartialEq<F>,
I: PartialEq<I>,
B: PartialEq<B>,
H: PartialEq<H>,
D: PartialEq<D>,
G: PartialEq<G>,
J: PartialEq<J>,
Implementors
impl<'_, A, B, const N: usize> PartialEq<&'_ [B; N]> for VecDeque<A> where
A: PartialEq<B>,
impl<'_, A, B, const N: usize> PartialEq<&'_ mut [B; N]> for VecDeque<A> where
A: PartialEq<B>,
impl<'_, T, U, A> PartialEq<Vec<U, A>> for Cow<'_, [T]> where
T: PartialEq<U> + Clone,
A: Allocator,
impl<'_, T, U, A, const N: usize> PartialEq<&'_ [U; N]> for Vec<T, A> where
T: PartialEq<U>,
A: Allocator,
impl<'a, 'b, B, C> PartialEq<Cow<'b, C>> for Cow<'a, B> where
C: ToOwned + ?Sized,
B: PartialEq<C> + ToOwned + ?Sized,
impl<B, C> PartialEq<ControlFlow<B, C>> for ControlFlow<B, C> where
C: PartialEq<C>,
B: PartialEq<B>,
impl<Idx> PartialEq<RangeInclusive<Idx>> for RangeInclusive<Idx> where
Idx: PartialEq<Idx>,
impl<Idx> PartialEq<RangeToInclusive<Idx>> for RangeToInclusive<Idx> where
Idx: PartialEq<Idx>,
impl<Y, R> PartialEq<GeneratorState<Y, R>> for GeneratorState<Y, R> where
R: PartialEq<R>,
Y: PartialEq<Y>,