Trait core::cmp::PartialEq

1.0.0 · source · []
pub trait PartialEq<Rhs: ?Sized = Self> {
    const fn eq(&self, other: &Rhs) -> bool;

    const 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).

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> and B: PartialEq<A>, then a == b implies b == a; and

  • Transitive: if A: PartialEq<B> and B: PartialEq<C> and A: PartialEq<C>, then a == b and b == c implies a == 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 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, 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);
Run

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);
Run

By changing impl PartialEq for Book to impl PartialEq<BookFormat> for Book, we allow BookFormats to be compared with Books.

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
}
Run

Examples

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

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

Required Methods

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

Provided Methods

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Implementors

This trait is implemented for function pointers with up to twelve arguments.

This trait is implemented for function pointers with up to twelve arguments.

This trait is implemented for function pointers with up to twelve arguments.

This trait is implemented for function pointers with up to twelve arguments.

This trait is implemented for function pointers with up to twelve arguments.

This trait is implemented for function pointers with up to twelve arguments.

This trait is implemented for tuples up to twelve items long.

impl<T: ?Sized + PartialEq, A: Allocator> PartialEq<Box<T, A>> for Box<T, A>

impl<'a, 'b, B: ?Sized, C: ?Sized> PartialEq<Cow<'b, C>> for Cow<'a, B>where
    B: PartialEq<C> + ToOwned,
    C: ToOwned,

impl<K: PartialEq, V: PartialEq, A: Allocator + Clone> PartialEq<BTreeMap<K, V, A>> for BTreeMap<K, V, A>

impl<T: PartialEq, A: Allocator + Clone> PartialEq<BTreeSet<T, A>> for BTreeSet<T, A>

impl<T: PartialEq, A: Allocator> PartialEq<VecDeque<T, A>> for VecDeque<T, A>

impl<T, U, A: Allocator> PartialEq<Vec<U, A>> for VecDeque<T, A>where
    T: PartialEq<U>,

impl<T, U, A: Allocator> PartialEq<&[U]> for VecDeque<T, A>where
    T: PartialEq<U>,

impl<T, U, A: Allocator> PartialEq<&mut [U]> for VecDeque<T, A>where
    T: PartialEq<U>,

impl<T, U, A: Allocator, const N: usize> PartialEq<[U; N]> for VecDeque<T, A>where
    T: PartialEq<U>,

impl<T, U, A: Allocator, const N: usize> PartialEq<&[U; N]> for VecDeque<T, A>where
    T: PartialEq<U>,

impl<T, U, A: Allocator, const N: usize> PartialEq<&mut [U; N]> for VecDeque<T, A>where
    T: PartialEq<U>,

impl<T: ?Sized + PartialEq> PartialEq<Rc<T>> for Rc<T>

impl<'a, 'b> PartialEq<str> for String

impl<'a, 'b> PartialEq<String> for str

impl<'a, 'b> PartialEq<&'a str> for String

impl<'a, 'b> PartialEq<String> for &'a str

impl<'a, 'b> PartialEq<str> for Cow<'a, str>

impl<'a, 'b> PartialEq<Cow<'a, str>> for str

impl<'a, 'b> PartialEq<&'b str> for Cow<'a, str>

impl<'a, 'b> PartialEq<Cow<'a, str>> for &'b str

impl<'a, 'b> PartialEq<String> for Cow<'a, str>

impl<'a, 'b> PartialEq<Cow<'a, str>> for String

impl<T: ?Sized + PartialEq> PartialEq<Arc<T>> for Arc<T>

impl<T, U, A1: Allocator, A2: Allocator> PartialEq<Vec<U, A2>> for Vec<T, A1>where
    T: PartialEq<U>,

impl<T, U, A: Allocator> PartialEq<&[U]> for Vec<T, A>where
    T: PartialEq<U>,

impl<T, U, A: Allocator> PartialEq<&mut [U]> for Vec<T, A>where
    T: PartialEq<U>,

impl<T, U, A: Allocator> PartialEq<Vec<U, A>> for &[T]where
    T: PartialEq<U>,

impl<T, U, A: Allocator> PartialEq<Vec<U, A>> for &mut [T]where
    T: PartialEq<U>,

impl<T, U, A: Allocator> PartialEq<[U]> for Vec<T, A>where
    T: PartialEq<U>,

impl<T, U, A: Allocator> PartialEq<Vec<U, A>> for [T]where
    T: PartialEq<U>,

impl<T, U, A: Allocator> PartialEq<Vec<U, A>> for Cow<'_, [T]>where
    T: PartialEq<U>,
    T: Clone,

impl<T, U> PartialEq<&[U]> for Cow<'_, [T]>where
    T: PartialEq<U>,
    T: Clone,

impl<T, U> PartialEq<&mut [U]> for Cow<'_, [T]>where
    T: PartialEq<U>,
    T: Clone,

impl<T, U, A: Allocator, const N: usize> PartialEq<[U; N]> for Vec<T, A>where
    T: PartialEq<U>,

impl<T, U, A: Allocator, const N: usize> PartialEq<&[U; N]> for Vec<T, A>where
    T: PartialEq<U>,

impl PartialEq<char> for Punct

impl PartialEq<Punct> for char

impl<K, V, S> PartialEq<HashMap<K, V, S>> for HashMap<K, V, S>where
    K: Eq + Hash,
    V: PartialEq,
    S: BuildHasher,

impl<T, S> PartialEq<HashSet<T, S>> for HashSet<T, S>where
    T: Eq + Hash,
    S: BuildHasher,

impl<'a> PartialEq<OsString> for &'a str

impl PartialEq<str> for OsStr

impl PartialEq<OsStr> for str

impl<'a, 'b> PartialEq<OsStr> for OsString

impl<'a, 'b> PartialEq<OsString> for OsStr

impl<'a, 'b> PartialEq<&'a OsStr> for OsString

impl<'a, 'b> PartialEq<OsString> for &'a OsStr

impl<'a, 'b> PartialEq<OsStr> for Cow<'a, OsStr>

impl<'a, 'b> PartialEq<Cow<'a, OsStr>> for OsStr

impl<'a, 'b> PartialEq<&'b OsStr> for Cow<'a, OsStr>

impl<'a, 'b> PartialEq<Cow<'a, OsStr>> for &'b OsStr

impl<'a, 'b> PartialEq<OsString> for Cow<'a, OsStr>

impl<'a, 'b> PartialEq<Cow<'a, OsStr>> for OsString

impl<T: PartialEq> PartialEq<Cursor<T>> for Cursor<T>

impl<'a> PartialEq<Prefix<'a>> for Prefix<'a>

impl<'a> PartialEq<Component<'a>> for Component<'a>

impl<'a> PartialEq<Components<'a>> for Components<'a>

impl PartialEq<Path> for Path

impl<'a, 'b> PartialEq<Path> for PathBuf

impl<'a, 'b> PartialEq<PathBuf> for Path

impl<'a, 'b> PartialEq<&'a Path> for PathBuf

impl<'a, 'b> PartialEq<PathBuf> for &'a Path

impl<'a, 'b> PartialEq<Path> for Cow<'a, Path>

impl<'a, 'b> PartialEq<Cow<'a, Path>> for Path

impl<'a, 'b> PartialEq<&'b Path> for Cow<'a, Path>

impl<'a, 'b> PartialEq<Cow<'a, Path>> for &'b Path

impl<'a, 'b> PartialEq<PathBuf> for Cow<'a, Path>

impl<'a, 'b> PartialEq<Cow<'a, Path>> for PathBuf

impl<'a, 'b> PartialEq<OsStr> for PathBuf

impl<'a, 'b> PartialEq<PathBuf> for OsStr

impl<'a, 'b> PartialEq<&'a OsStr> for PathBuf

impl<'a, 'b> PartialEq<PathBuf> for &'a OsStr

impl<'a, 'b> PartialEq<Cow<'a, OsStr>> for PathBuf

impl<'a, 'b> PartialEq<PathBuf> for Cow<'a, OsStr>

impl<'a, 'b> PartialEq<OsString> for PathBuf

impl<'a, 'b> PartialEq<PathBuf> for OsString

impl<'a, 'b> PartialEq<OsStr> for Path

impl<'a, 'b> PartialEq<Path> for OsStr

impl<'a, 'b> PartialEq<&'a OsStr> for Path

impl<'a, 'b> PartialEq<Path> for &'a OsStr

impl<'a, 'b> PartialEq<Cow<'a, OsStr>> for Path

impl<'a, 'b> PartialEq<Path> for Cow<'a, OsStr>

impl<'a, 'b> PartialEq<OsString> for Path

impl<'a, 'b> PartialEq<Path> for OsString

impl<'a, 'b> PartialEq<OsStr> for &'a Path

impl<'a, 'b> PartialEq<&'a Path> for OsStr

impl<'a, 'b> PartialEq<Cow<'b, OsStr>> for &'a Path

impl<'a, 'b> PartialEq<&'a Path> for Cow<'b, OsStr>

impl<'a, 'b> PartialEq<OsString> for &'a Path

impl<'a, 'b> PartialEq<&'a Path> for OsString

impl<'a, 'b> PartialEq<OsStr> for Cow<'a, Path>

impl<'a, 'b> PartialEq<Cow<'a, Path>> for OsStr

impl<'a, 'b> PartialEq<&'b OsStr> for Cow<'a, Path>

impl<'a, 'b> PartialEq<Cow<'a, Path>> for &'b OsStr

impl<'a, 'b> PartialEq<OsString> for Cow<'a, Path>

impl<'a, 'b> PartialEq<Cow<'a, Path>> for OsString

impl<T: PartialEq> PartialEq<SendError<T>> for SendError<T>

impl<T: PartialEq> PartialEq<OnceLock<T>> for OnceLock<T>