Trait void::coreprovider::cmp::PartialEq1.0.0[][src]

#[lang = "eq"]
pub trait PartialEq<Rhs = Self> where
    Rhs: ?Sized
{
#[must_use]
fn eq(&self, other: &Rhs) -> bool;
#[must_use]
fn ne(&self, other: &Rhs) -> bool { ... } }

Trait for equality comparisons which are partial equivalence relations.

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.

Formally, the equality must be (for all a, b and c):

Note that these requirements mean that the trait itself must be implemented symmetrically and transitively: if T: PartialEq<U> and U: PartialEq<V> then U: PartialEq<T> and T: PartialEq<V>.

Derivable

This trait can be used with #[derive]. When derived on structs, two instances are equal if all fields are equal, and not equal if any fields are not equal. When derived on enums, each variant is equal to itself and not equal to the other variants.

How can I implement PartialEq?

PartialEq only requires the eq method to be implemented; ne is defined in terms of it by default. Any manual implementation of ne must respect the rule that eq is a strict inverse of ne; that is, !(a == b) if and only if a != b.

Implementations of PartialEq, PartialOrd, and Ord must agree with each other. It's easy to accidentally make them disagree by deriving some of the traits and manually implementing others.

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: &Book) -> 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);

Examples

let x: u32 = 0;
let y: u32 = 1;

assert_eq!(x == y, false);
assert_eq!(x.eq(&y), false);

Required Methods

This method tests for self and other values to be equal, and is used by ==.

Provided Methods

This method tests for !=.

Implementations on Foreign Types

impl PartialEq<NonZeroUsize> for NonZeroUsize
[src]

impl PartialEq<Pinned> for Pinned
[src]

impl<T> PartialEq<Discriminant<T>> for Discriminant<T>
[src]

impl PartialEq<Duration> for Duration
[src]

impl<T> PartialEq<Option<T>> for Option<T> where
    T: PartialEq<T>, 
[src]

impl PartialEq<AllocErr> for AllocErr
[src]

impl PartialEq<DecodeUtf16Error> for DecodeUtf16Error
[src]

impl PartialEq<NonZeroU32> for NonZeroU32
[src]

impl<T> PartialEq<Bound<T>> for Bound<T> where
    T: PartialEq<T>, 
[src]

impl PartialEq<NonZeroU64> for NonZeroU64
[src]

impl PartialEq<UnicodeVersion> for UnicodeVersion
[src]

impl PartialEq<NonZeroU16> for NonZeroU16
[src]

impl PartialEq<CharTryFromError> for CharTryFromError
[src]

impl PartialEq<CannotReallocInPlace> for CannotReallocInPlace
[src]

impl PartialEq<NonZeroU8> for NonZeroU8
[src]

impl<Idx> PartialEq<Range<Idx>> for Range<Idx> where
    Idx: PartialEq<Idx>, 
[src]

impl<Idx> PartialEq<RangeToInclusive<Idx>> for RangeToInclusive<Idx> where
    Idx: PartialEq<Idx>, 
[src]

impl PartialEq<SearchStep> for SearchStep
[src]

impl PartialEq<NoneError> for NoneError
[src]

impl PartialEq<Utf8Error> for Utf8Error
[src]

impl<T> PartialEq<NonNull<T>> for NonNull<T> where
    T: ?Sized
[src]

impl<T, E> PartialEq<Result<T, E>> for Result<T, E> where
    E: PartialEq<E>,
    T: PartialEq<T>, 
[src]

impl PartialEq<InvalidSequence> for InvalidSequence
[src]

impl<Y, R> PartialEq<GeneratorState<Y, R>> for GeneratorState<Y, R> where
    R: PartialEq<R>,
    Y: PartialEq<Y>, 
[src]

impl<Idx> PartialEq<RangeFrom<Idx>> for RangeFrom<Idx> where
    Idx: PartialEq<Idx>, 
[src]

impl PartialEq<TypeId> for TypeId
[src]

impl PartialEq<ParseIntError> for ParseIntError
[src]

impl<Idx> PartialEq<RangeInclusive<Idx>> for RangeInclusive<Idx> where
    Idx: PartialEq<Idx>, 
[src]

impl<T> PartialEq<Wrapping<T>> for Wrapping<T> where
    T: PartialEq<T>, 
[src]

impl PartialEq<FpCategory> for FpCategory
[src]

impl<'a> PartialEq<Utf8LossyChunk<'a>> for Utf8LossyChunk<'a>
[src]

impl<H> PartialEq<BuildHasherDefault<H>> for BuildHasherDefault<H>
[src]

impl<T> PartialEq<ManuallyDrop<T>> for ManuallyDrop<T> where
    T: PartialEq<T>, 
[src]

impl PartialEq<ParseCharError> for ParseCharError
[src]

impl PartialEq<ParseFloatError> for ParseFloatError
[src]

impl<T> PartialEq<Cell<T>> for Cell<T> where
    T: Copy + PartialEq<T>, 
[src]

impl<T> PartialEq<Poll<T>> for Poll<T> where
    T: PartialEq<T>, 
[src]

impl PartialEq<Layout> for Layout
[src]

impl<T> PartialEq<PhantomData<T>> for PhantomData<T> where
    T: ?Sized
[src]

impl<T> PartialEq<RefCell<T>> for RefCell<T> where
    T: PartialEq<T> + ?Sized
[src]

Panics

Panics if the value in either RefCell is currently borrowed.

impl PartialEq<LayoutErr> for LayoutErr
[src]

impl PartialEq<NonZeroU128> for NonZeroU128
[src]

impl PartialEq<RangeFull> for RangeFull
[src]

impl PartialEq<ParseBoolError> for ParseBoolError
[src]

impl<Idx> PartialEq<RangeTo<Idx>> for RangeTo<Idx> where
    Idx: PartialEq<Idx>, 
[src]

Implementors