Trait std::cmp::Eq

1.0.0 · source · []
pub trait Eq: PartialEq<Self> { }
Expand description

Trait for equality comparisons which are equivalence relations.

This means, that in addition to a == b and a != b being strict inverses, the equality must be (for all a, b and c):

  • reflexive: a == a;
  • symmetric: a == b implies b == a; and
  • transitive: a == b and b == c implies a == c.

This property cannot be checked by the compiler, and therefore Eq implies PartialEq, and has no extra methods.

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

Implementations on Foreign Types

Implementors

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

impl<B: ?Sized> Eq for Cow<'_, B> where
    B: Eq + ToOwned

impl<K: Eq, V: Eq> Eq for BTreeMap<K, V>

impl<T: Eq> Eq for BTreeSet<T>

impl<T: Eq> Eq for LinkedList<T>

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

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

impl Eq for String

impl Eq for FromUtf8Error

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

impl<T: Eq, A: Allocator> Eq for Vec<T, A>

impl Eq for LineColumn

impl Eq for SourceFile

impl Eq for Delimiter

impl Eq for Spacing

impl Eq for Concurrent

impl Eq for ShouldPanic

impl Eq for OutputFormat

impl Eq for RunIgnored

impl Eq for TestType

impl Eq for NamePadding

impl Eq for TestName

impl Eq for TestId