Eq

Trait Eq 

1.6.0 (const: unstable) · Source
pub trait Eq: PartialEq<Self> + PointeeSized { }
Expand description

Trait for comparisons corresponding to equivalence relations.

The primary difference to PartialEq is the additional requirement for reflexivity. A type that implements PartialEq guarantees that for all a, b and c:

  • symmetric: a == b implies b == a and a != b implies !(a == b)
  • transitive: a == b and b == c implies a == c

Eq, which builds on top of PartialEq also implies:

  • reflexive: a == a

This property cannot be checked by the compiler, and therefore Eq is a trait without methods.

Violating this property 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.

Floating point types such as f32 and f64 implement only PartialEq but not Eq because NaN != NaN.

§Derivable

This trait can be used with #[derive]. When derived, because Eq has no extra methods, it is only informing the compiler that this is an equivalence relation rather than a partial equivalence relation. Note that the derive strategy requires all fields are Eq, which isn’t always desired.

§How can I implement Eq?

If you cannot use the derive strategy, specify that your type implements Eq, which has no extra methods:

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

impl Eq for Book {}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl Eq for AtomicOrdering

1.0.0 (const: unstable) · Source§

impl Eq for bool

1.0.0 (const: unstable) · Source§

impl Eq for i8

1.0.0 (const: unstable) · Source§

impl Eq for i16

1.0.0 (const: unstable) · Source§

impl Eq for i32

1.0.0 (const: unstable) · Source§

impl Eq for i64

1.0.0 (const: unstable) · Source§

impl Eq for i128

1.0.0 (const: unstable) · Source§

impl Eq for isize

1.0.0 (const: unstable) · Source§

impl Eq for u8

1.0.0 (const: unstable) · Source§

impl Eq for u16

1.0.0 (const: unstable) · Source§

impl Eq for u32

1.0.0 (const: unstable) · Source§

impl Eq for u64

1.0.0 (const: unstable) · Source§

impl Eq for u128

1.0.0 (const: unstable) · Source§

impl Eq for usize

1.0.0 · Source§

impl<T: PointeeSized> Eq for *const T

Pointer equality is an equivalence relation.