# Trait tracing::stdlib::cmp::PartialEq

1.0.0 · source ·
``````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 equality comparisons.

`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`. 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>` 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.

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

### 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§

source

#### fn eq(&self, other: &Rhs) -> bool

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

## Provided Methods§

source

#### fn ne(&self, other: &Rhs) -> bool

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

## Implementors§

1.29.0 · source§

source§

source§

source§

source§

source§

1.65.0 · source§

source§

1.34.0 · source§

source§

1.28.0 · source§

source§

source§

1.7.0 · source§

1.16.0 · source§

1.16.0 · source§

source§

source§

source§

source§

1.55.0 · source§

source§

source§

source§

source§

1.12.0 · source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

1.28.0 · source§

1.50.0 · source§

source§

1.27.0 · source§

1.34.0 · source§

1.9.0 · source§

1.20.0 · source§

1.59.0 · source§

1.57.0 · source§

source§

1.64.0 · source§

1.69.0 · source§

1.64.0 · source§

1.64.0 · source§

1.64.0 · source§

1.64.0 · source§

source§

source§

1.8.0 · source§

1.8.0 · source§

source§

source§

1.8.0 · source§

1.8.0 · source§

source§

1.1.0 · source§

source§

1.33.0 · source§

source§

source§

1.16.0 · source§

source§

1.16.0 · source§

source§

source§

source§

1.34.0 · source§

1.34.0 · source§

1.34.0 · source§

1.34.0 · source§

1.34.0 · source§

1.34.0 · source§

1.28.0 · source§

1.28.0 · source§

1.28.0 · source§

1.28.0 · source§

1.28.0 · source§

1.28.0 · source§

source§

source§

1.34.0 · source§

source§

source§

1.8.0 · source§

1.8.0 · source§

source§

1.6.0 · source§

1.8.0 · source§

1.8.0 · source§

1.6.0 · source§

source§

1.7.0 · source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

1.5.0 · source§

1.36.0 · source§

1.36.0 · source§

1.26.0 · source§

1.19.0 · source§

1.3.0 · source§

1.8.0 · source§

1.8.0 · source§

1.66.0 · source§

1.8.0 · source§

1.8.0 · source§

1.8.0 · source§

1.8.0 · source§

1.6.0 · source§

1.8.0 · source§

1.8.0 · source§

1.8.0 · source§

1.8.0 · source§

1.6.0 · source§

1.6.0 · source§

source§

source§

source§

source§

source§

1.8.0 · source§

1.8.0 · source§

1.29.0 · source§

1.8.0 · source§

1.8.0 · source§

1.10.0 · source§

source§

1.8.0 · source§

1.8.0 · source§

1.6.0 · source§

1.8.0 · source§

1.6.0 · source§

1.8.0 · source§

1.6.0 · source§

source§

source§

source§

1.8.0 · source§

1.8.0 · source§

source§

1.8.0 · source§

1.8.0 · source§

1.6.0 · source§

source§

source§

source§

1.8.0 · source§

1.8.0 · source§

1.8.0 · source§

1.8.0 · source§

1.6.0 · source§

1.8.0 · source§

source§

source§

1.8.0 · source§

1.8.0 · source§

1.8.0 · source§

1.8.0 · source§

1.8.0 · source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

1.55.0 · source§

source§

1.4.0 · source§

1.29.0 · source§

source§

source§

1.26.0 · source§

source§

1.26.0 · source§

source§

source§

1.41.0 · source§

1.17.0 · source§

source§

source§

1.36.0 · source§

source§

source§

source§

### impl<T> PartialEq<(T,)> for (T₁, T₂, …, Tₙ)where T: PartialEq<T> + ?Sized,

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

source§

source§

source§

1.70.0 · source§

source§

source§

source§

1.21.0 · source§

1.20.0 · source§

1.74.0 · source§

source§

1.25.0 · source§

source§

1.70.0 · source§

1.19.0 · source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

source§

1.17.0 · source§

source§

1.17.0 · source§

source§

1.48.0 · source§

1.46.0 · source§

1.46.0 · source§

source§

1.48.0 · source§

1.17.0 · source§

1.17.0 · source§

source§

1.17.0 · source§

1.17.0 · source§

source§

source§