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


Rust HashSet.difference用法及代碼示例


本文簡要介紹rust語言中 std::collections::hash_set::HashSet.difference 的用法。

用法

pub fn difference<'a>(    &'a self,     other: &'a HashSet<T, S>) -> Difference<'a, T, S>

訪問表示差異的值,即在 self 但不在 other 中的值。

例子

use std::collections::HashSet;
let a: HashSet<_> = [1, 2, 3].iter().cloned().collect();
let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();

// Can be seen as `a - b`.
for x in a.difference(&b) {
    println!("{}", x); // Print 1
}

let diff: HashSet<_> = a.difference(&b).collect();
assert_eq!(diff, [1].iter().collect());

// Note that difference is not symmetric,
// and `b - a` means something else:
let diff: HashSet<_> = b.difference(&a).collect();
assert_eq!(diff, [4].iter().collect());

相關用法


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