Channels ](../../std_misc/channels.html)
    4. [ **20.3.** Path ](../../std_misc/path.html)
    5. [ **20.4.** File I/O ](../../std_misc/file.html) â± 
    6.       1. [ **20.4.1.** open ](../../std_misc/file/open.html)
      2. [ **20.4.2.** create ](../../std_misc/file/create.html)
      3. [ **20.4.3.** read lines ](../../std_misc/file/read_lines.html)
    7. [ **20.5.** Child processes ](../../std_misc/process.html) â± 
    8.       1. [ **20.5.1.** Pipes ](../../std_misc/process/pipe.html)
      2. [ **20.5.2.** Wait ](../../std_misc/process/wait.html)
    9. [ **20.6.** Filesystem Operations ](../../std_misc/fs.html)
    10. [ **20.7.** Program arguments ](../../std_misc/arg.html) â± 
    11.       1. [ **20.7.1.** Argument parsing ](../../std_misc/arg/matching.html)
    12. [ **20.8.** Foreign Function Interface ](../../std_misc/ffi.html)
  41. [ **21.** Testing ](../../testing.html) â± 
  42.     1. [ **21.1.** Unit testing ](../../testing/unit_testing.html)
    2. [ **21.2.** Documentation testing ](../../testing/doc_testing.html)
    3. [ **21.3.** Integration testing ](../../testing/integration_testing.html)
    4. [ **21.4.** Dev-dependencies ](../../testing/dev_dependencies.html)
  43. [ **22.** Unsafe Operations ](../../unsafe.html) â± 
  44.     1. [ **22.1.** Inline assembly ](../../unsafe/asm.html)
  45. [ **23.** Compatibility ](../../compatibility.html) â± 
  46.     1. [ **23.1.** Raw identifiers ](../../compatibility/raw_identifiers.html)
  47. [ **24.** Meta ](../../meta.html) â± 
  48.     1. [ **24.1.** Documentation ](../../meta/doc.html)
    2. [ **24.2.** Playground ](../../meta/playground.html)

__ __

  * Light (default) 
  * Rust 
  * Coal 
  * Navy 
  * Ayu 

__

#  Rust By Example

[ __ ](../../print.html "Print this book") [ __ ](https://github.com/rust-
lang/rust-by-example "Git repository") [ __ ](https://github.com/rust-
lang/rust-by-example/edit/master/src/std/hash/hashset.md "Suggest an edit")

#  HashSet

Consider a ` HashSet ` as a ` HashMap ` where we just care about the keys ( `
HashSet<T> ` is, in actuality, just a wrapper around ` HashMap<T, ()> ` ).

"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 collection fulfills. ` HashSet ` is
just one implementation. (see also: [ ` BTreeSet ` ](https://doc.rust-
lang.org/std/collections/struct.BTreeSet.html) )

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.

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

  * ` union ` : get all the unique elements in both sets. 

  * ` 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<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, ele