HashSet
可以将 HashSet
看作是一个只关心键的 HashMap
(实际上,HashSet<T>
只是 HashMap<T, ()>
的一个包装器)。
你可能会问:“这有什么意义?我可以直接将键存储在 Vec
中。”
HashSet
的独特之处在于,它保证不会有重复的元素。这是任何集合类型都必须满足的约定。HashSet
只是其中一种实现。(另请参阅:BTreeSet
)
如果插入的值已经存在于 HashSet
中(即新值与现有值相等,并且它们的哈希值相同),那么新值将替换旧值。
当您永远不想要多个相同的东西,或者当您想知道是否已经拥有某个东西时,这非常有用。
但集合的功能不止于此。
集合有 4 个主要操作(以下所有调用都返回一个迭代器):
-
union
:获取两个集合中的所有唯一元素。 -
difference
:获取存在于第一个集合中但不存在于第二个集合中的所有元素。 -
intersection
:获取仅存在于两个集合中的所有元素。 -
symmetric_difference
:获取存在于其中一个集合中或另一个集合中,但不是同时存在于两个集合中的所有元素。
在下面的示例中尝试所有这些操作
use std::collections::HashSet; fn main() { let mut a: HashSet<i32> = vec![1i32, 2, 3].into_iter().collect(); let mut b: HashSet<i32> = vec![2i32, 3, 4].into_iter().collect(); assert!(a.insert(4)); assert!(a.contains(&4)); // `HashSet::insert()` returns false if // there was a value already present. assert!(b.insert(4), "Value 4 is already in set B!"); // FIXME ^ Comment out this line b.insert(5); // If a collection's element type implements `Debug`, // then the collection implements `Debug`. // It usually prints its elements in the format `[elem1, elem2, ...]` println!("A: {:?}", a); println!("B: {:?}", b); // Print [1, 2, 3, 4, 5] in arbitrary order println!("Union: {:?}", a.union(&b).collect::<Vec<&i32>>()); // This should print [1] println!("Difference: {:?}", a.difference(&b).collect::<Vec<&i32>>()); // Print [2, 3, 4] in arbitrary order. println!("Intersection: {:?}", a.intersection(&b).collect::<Vec<&i32>>()); // Print [1, 5] println!("Symmetric Difference: {:?}", a.symmetric_difference(&b).collect::<Vec<&i32>>()); }
(示例改编自文档。)