Trait core::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

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 + Eq, A: Allocator> Eq for Box<T, A>

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

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

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

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

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

impl Eq for CString

impl Eq for NulError

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 AccessError

impl Eq for ThreadId

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

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

impl Eq for VarError

impl Eq for OsString

impl Eq for OsStr

impl Eq for Permissions

impl Eq for FileType

impl<T: Eq> Eq for Cursor<T>

impl Eq for ErrorKind

impl Eq for SeekFrom

impl Eq for IpAddr

impl Eq for Ipv4Addr

impl Eq for Ipv6Addr

impl Eq for SocketAddr

impl Eq for SocketAddrV4

impl Eq for SocketAddrV6

impl Eq for Shutdown

impl Eq for UCred

impl<'a> Eq for Prefix<'a>

impl<'a> Eq for PrefixComponent<'a>

impl<'a> Eq for Component<'a>

impl Eq for Components<'_>

impl Eq for PathBuf

impl Eq for Path

impl Eq for Output

impl Eq for ExitStatus

impl<T: Eq> Eq for SendError<T>

impl Eq for RecvError

impl Eq for TryRecvError

impl<T: Eq> Eq for TrySendError<T>

impl<T: Eq> Eq for OnceLock<T>

impl Eq for Instant

impl Eq for SystemTime

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