博客
关于我
Rust 内置 trait :PartialEq 和 Eq
阅读量:416 次
发布时间:2019-03-06

本文共 2999 字,大约阅读时间需要 9 分钟。

GitHub:

Emai: debugzhang@163.com

华为云社区:

and are traits that allow you to define total and partial equality between values, respectively. Implementing them overloads the == and != operators.

PartialEq

/// [`eq`]: PartialEq::eq/// [`ne`]: PartialEq::ne#[lang = "eq"]#[stable(feature = "rust1", since = "1.0.0")]#[doc(alias = "==")]#[doc(alias = "!=")]#[rustc_on_unimplemented(    message = "can't compare `{Self}` with `{Rhs}`",    label = "no implementation for `{Self} == {Rhs}`")]pub trait PartialEq
{ /// This method tests for `self` and `other` values to be equal, and is used /// by `==`. #[must_use] #[stable(feature = "rust1", since = "1.0.0")] fn eq(&self, other: &Rhs) -> bool; /// This method tests for `!=`. #[inline] #[must_use] #[stable(feature = "rust1", since = "1.0.0")] fn ne(&self, other: &Rhs) -> bool { !self.eq(other) }}

如果我们想比较某个类型的两个值 x 和 y 是否相等(不等),例如:x == y (x != y),那么我们就必须为类型实现 PartialEq Trait

PartialEq 可使用 #[derive] 来交由编译器实现,当一个 struct 在进行相等比较时,会对其中每一个字段进行比较;如果遇到枚举时,还会对枚举所拥有的数据进行比较。

我们也可以自己实现 PartialEq,实现时只需要实现判断是否相等的函数 fn eq(&self, other: &Self) -> bool ,Rust 会自动提供 fn ne(&self, other: &Self) -> bool。例子如下:

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

Eq

pub trait Eq: PartialEq
{ // this method is used solely by #[deriving] to assert // that every component of a type implements #[deriving] // itself, the current deriving infrastructure means doing this // assertion without using a method on this trait is nearly // impossible. // // This should never be implemented by hand. #[doc(hidden)] #[inline] #[stable(feature = "rust1", since = "1.0.0")] fn assert_receiver_is_total_eq(&self) {}}

实现 Eq 的前提是已经实现了 PartialEq,因为实现 Eq 不需要额外的代码,只需要在实现了PartialEq 的基础上告诉编译器它的比较满足自反性就可以了。对于上面的例子只需要:#[derive(Eq)]impl Eq for Book {}

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

PartialEq 和 Eq

这两个 Traits 的名称实际上来自于抽象代数中的等价关系和局部等价关系。

等价关系(equivalence relation)即设 \(\displaystyle R\) 是某个集合 \(\displaystyle A\) 上的一个二元关系。若 \(\displaystyle R\) 满足以下条件:

  1. 自反性:\(\displaystyle \forall x\in A,~~xRx\)
  2. 对称性:\(\displaystyle \forall x,y\in A,~~xRy~~\implies ~~yRx\)
  3. 传递性:\(\displaystyle \forall x,y,z\in A,~~~(xRy~~\wedge ~~yRz)~~\implies ~~xRz\)

则称 \(\displaystyle R\) 是一个定义在 \(\displaystyle A\) 上的等价关系

并非所有的二元关系都是等价关系, EqPartialEq 的区别在于是否在相等比较中是否满足自反性,即 x == x

例如对于浮点类型,Rust 只实现了 PartialEq 而没有实现 Eq,原因在于 NaN != Nan,不满足自反性。

Eq 相比 PartialEq 需要额外满足自反性,即 a == a,对于浮点类型,Rust 只实现了 PartialEq 而不是 Eq,原因就是 NaN != NaN

Eq 和 Hash

当一个类型同时实现了 Eq 和 Hash 时,该类型满足下列特性:

k1 == k2 -> hash(k1) == hash(k2)

即,当两个 key 相等时,它们的哈希值必然相等。Rust 里的 HashMap 和 HashSet 都依赖该特性。

转载地址:http://srkuz.baihongyu.com/

你可能感兴趣的文章
Mysql中怎样设置指定ip远程访问连接
查看>>
mysql中数据表的基本操作很难嘛,由这个实验来带你从头走一遍
查看>>
Mysql中文乱码问题完美解决方案
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
Mysql中的 IFNULL 函数的详解
查看>>
mysql中的collate关键字是什么意思?
查看>>
MySql中的concat()相关函数
查看>>
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>
MySQL中的count函数
查看>>
MySQL中的DB、DBMS、SQL
查看>>
MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
查看>>
MySQL中的GROUP_CONCAT()函数详解与实战应用
查看>>
MySQL中的IO问题分析与优化
查看>>
MySQL中的ON DUPLICATE KEY UPDATE详解与应用
查看>>
mysql中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
查看>>
mysql中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
查看>>
mysql中的字段如何选择合适的数据类型呢?
查看>>