當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Rust Ordering.then_with用法及代碼示例


本文簡要介紹rust語言中 core::cmp::Ordering.then_with 的用法。

用法

pub fn then_with<F: FnOnce() -> Ordering>(self, f: F) -> Ordering

將排序與給定函數鏈接起來。

當它不是 Equal 時返回 self 。否則調用f 並返回結果。

例子

use std::cmp::Ordering;

let result = Ordering::Equal.then_with(|| Ordering::Less);
assert_eq!(result, Ordering::Less);

let result = Ordering::Less.then_with(|| Ordering::Equal);
assert_eq!(result, Ordering::Less);

let result = Ordering::Less.then_with(|| Ordering::Greater);
assert_eq!(result, Ordering::Less);

let result = Ordering::Equal.then_with(|| Ordering::Equal);
assert_eq!(result, Ordering::Equal);

let x: (i64, i64, i64) = (1, 2, 7);
let y: (i64, i64, i64) = (1, 5, 3);
let result = x.0.cmp(&y.0).then_with(|| x.1.cmp(&y.1)).then_with(|| x.2.cmp(&y.2));

assert_eq!(result, Ordering::Less);

相關用法


注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 core::cmp::Ordering.then_with。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。