Trait std::cmp::Eq1.0.0[][src]

pub trait Eq: PartialEq<Self> { }
Expand description

等价关系 等式比较的 Trait。

这意味着,除了 a == ba != b 是严格的逆之外,相等必须是 (对于所有 abc) :

  • reflexive: a == a;
  • 对称: a == b 表示 b == a; and
  • 可传递的: a == bb == c 表示 a == c

编译器无法检查此属性,因此 Eq 表示 PartialEq,并且没有其他方法。

Derivable

该 trait 可以与 #[derive] 一起使用。 当 derived' 时,由于 Eq` 没有额外的方法,它只是通知编译器这是一个等价关系,而不是部分等价关系。

请注意,derive 策略要求所有字段均为 Eq,这并不总是需要的。

如何实现 Eq?

如果您不能使用 derive 策略,请指定您的类型实现 Eq,它没有方法:

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