本文簡要介紹rust語言中 Trait std::cmp::PartialOrd 的用法。
用法
pub trait PartialOrd<Rhs = Self>: PartialEq<Rhs> where Rhs: ?Sized, {
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 > cpartial_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.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 PathBuf.set_file_name用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 Trait std::cmp::PartialOrd。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
