本文简要介绍rust语言中 Trait core::cmp::PartialOrd 的用法。
用法
pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
    fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>;
    fn lt(&self, other: &Rhs) -> bool { ... }
    fn le(&self, other: &Rhs) -> bool { ... }
    fn gt(&self, other: &Rhs) -> bool { ... }
    fn ge(&self, other: &Rhs) -> bool { ... }
}可以比较 sort-order 的值的特征。
可以分别使用 < 、 <= 、 > 和 >= 运算符调用此特征的 lt 、 le 、 gt 和 ge 方法。
此 trait 的方法必须彼此一致,并且在以下意义上与 PartialEq 的方法一致:
- a == b当且仅当- partial_cmp(a, b) == Some(Equal)。
- a < b当且仅当- partial_cmp(a, b) == Some(Less)(由默认实现确保)。
- a > b当且仅当- partial_cmp(a, b) == Some(Greater)(由默认实现确保)。
- a <= b当且仅当- a < b || a == b(由默认实现确保)。
- a >= b当且仅当- a > b || a == b(由默认实现确保)。
- a != b当且仅当- !(a == b)(已经是- PartialEq的一部分)。
如果  Ord  也为 Self 和 Rhs 实现,它也必须与 partial_cmp 一致(有关确切要求,请参阅该特征的文档)。通过派生一些特征并手动实现其他特征,很容易意外地使他们不同意。
对于所有 a 、 b 和 c ,比较必须满足:
- 传递性: a < b和b < c意味着a < c。对于==和>也必须如此。
- 对偶性: a < b当且仅当b > a。
请注意,这些要求意味着特征本身必须对称且传递地实现: if T: PartialOrd<U> and U: PartialOrd<V> then U: PartialOrd<T> and T: PartialOrd<V> 。
推论
以下推论来自上述要求:
- <和- >的反自反性:- !(a < a)、- !(a > a)
- >的传递性:如果- a > b和- b > c则- a > c
- partial_cmp的二元性:- partial_cmp(a, b) == partial_cmp(b, a).map(Ordering::reverse)
可导出的
此特征可与 #[derive] 一起使用。当 derive d 在结构上时,它将根据结构成员的从上到下的声明顺序生成字典顺序。当 derive d 在枚举上时,变体按其从上到下的判别顺序排序。
如何实现 PartialOrd ?
PartialOrd 只需要实现 partial_cmp  方法,其他方法由默认实现生成。
但是,对于没有总顺序的类型,仍然可以单独实现其他类型。例如,对于浮点数,NaN < 0 == false 和 NaN >= 0 == false(参见 IEEE 754-2008 第 5.11 节)。
PartialOrd 要求您的类型为  PartialEq  。
如果您的类型是  Ord  ,您可以使用  cmp  来实现  partial_cmp  :
use std::cmp::Ordering;
#[derive(Eq)]
struct Person {
    id: u32,
    name: String,
    height: u32,
}
impl PartialOrd for Person {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for Person {
    fn cmp(&self, other: &Self) -> Ordering {
        self.height.cmp(&other.height)
    }
}
impl PartialEq for Person {
    fn eq(&self, other: &Self) -> bool {
        self.height == other.height
    }
}您可能还会发现在类型的字段上使用  partial_cmp  很有用。以下是具有浮点 height 字段的 Person 类型的示例,该字段是唯一用于排序的字段:
use std::cmp::Ordering;
struct Person {
    id: u32,
    name: String,
    height: f64,
}
impl PartialOrd for Person {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.height.partial_cmp(&other.height)
    }
}
impl PartialEq for Person {
    fn eq(&self, other: &Self) -> bool {
        self.height == other.height
    }
}例子
let x : u32 = 0;
let y : u32 = 1;
assert_eq!(x < y, true);
assert_eq!(x.lt(&y), true);相关用法
- Rust PartialOrd.partial_cmp用法及代码示例
- Rust PartialOrd用法及代码示例
- Rust PartialOrd.lt用法及代码示例
- Rust PartialOrd.le用法及代码示例
- Rust PartialOrd.ge用法及代码示例
- Rust PartialOrd.gt用法及代码示例
- Rust PartialEq用法及代码示例
- Rust ParseFloatError用法及代码示例
- Rust ParseIntError用法及代码示例
- Rust PanicInfo.payload用法及代码示例
- Rust Path.components用法及代码示例
- Rust PathBuf.with_capacity用法及代码示例
- Rust Path.is_symlink用法及代码示例
- Rust Path.canonicalize用法及代码示例
- Rust Path.is_relative用法及代码示例
- Rust Path.file_stem用法及代码示例
- Rust Path.to_string_lossy用法及代码示例
- Rust Path.display用法及代码示例
- Rust PathBuf.into_os_string用法及代码示例
- Rust PanicInfo用法及代码示例
- Rust PathBuf.pop用法及代码示例
- Rust Path.ancestors用法及代码示例
- Rust Path用法及代码示例
- Rust Path.is_dir用法及代码示例
- Rust Path.strip_prefix用法及代码示例
注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 Trait core::cmp::PartialOrd。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
