通过例子学习Rust

49.10.2 哈希集(HashSet)

Consider a HashSet as a HashMap where the key and value are the same. Or, to be more precise, a HashMap where we just care about the keys.

"What's the point of that?" you ask. "I could just store the keys in a Vec."

A HashSet's unique feature is that it is guaranteed to not have duplicate elements. That's the contract that any Set implementation fulfills. HashSet is just one implementation. (see also: MutableSet)

If you insert a value that is already present in the HashSet, (i.e. the new value is equal to the existing and they both have the same hash), then the new value will replace the old.

This is great for when you never want more than one of something, or when you want to know if you've already got something.

But sets can do more than that. If they weren't important, then why would there be a branch of mathematics dedicated to them?

Sets have 4 primary operations (all of the following calls return an iterator):

  • union: get all the elements in one set or the other.

  • difference: get all the elements that are in the first set but not the second.

  • intersection: get all the elements that are only in both sets.

  • symmetric_difference: get all the elements that are in one set or the other, but not both.

Try all of these in the following example.

use std::collections::HashSet; fn main() { let mut a: HashSet<int> = vec!(1i, 2, 3).into_iter().collect(); let mut b: HashSet<int> = vec!(2i, 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 `Show`, // then the collection implements `Show`. // 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<&int>>()); // This should print [1] println!("Difference: {}", a.difference(&b).collect::<Vec<&int>>()); // Print [2, 3, 4] in arbitrary order. println!("Intersection: {}", a.intersection(&b).collect::<Vec<&int>>()); // Print [1, 5] println!("Symmetric Difference: {}", a.symmetric_difference(&b).collect::<Vec<&int>>()); }

Sets don't seem so pointless now, do they?

(Examples adapted from the documentation.)