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.md "Suggest an edit")

#  HashMap

Where vectors store values by an integer index, ` HashMap ` s store values by
key. ` HashMap ` keys can be booleans, integers, strings, or any other type
that implements the ` Eq ` and ` Hash ` traits. More on this in the next
section.

Like vectors, ` HashMap ` s are growable, but HashMaps can also shrink
themselves when they have excess space. You can create a HashMap with a
certain starting capacity using ` HashMap::with_capacity(uint) ` , or use `
HashMap::new() ` to get a HashMap with a default initial capacity
(recommended).

    
    
    use std::collections::HashMap;
    
    fn call(number: &str) -> &str {
        match number {
            "798-1364" => "We're sorry, the call cannot be completed as dialed. 
                Please hang up and try again.",
            "645-7689" => "Hello, this is Mr. Awesome's Pizza. My name is Fred.
                What can I get for you today?",
            _ => "Hi! Who is this again?"
        }
    }
    
    fn main() { 
        let mut contacts = HashMap::new();
    
        contacts.insert("Daniel", "798-1364");
        contacts.insert("Ashley", "645-7689");
        contacts.insert("Katie", "435-8291");
        contacts.insert("Robert", "956-1745");
    
        // Takes a reference and returns Option<&V>
        match contacts.get(&"Daniel") {
            Some(&number) => println!("Calling Daniel: {}", call(number)),
            _ => println!("Don't have Daniel's number."),
        }
    
        // `HashMap::insert()` returns `None`
        // if the inserted value is new, `Some(value)` otherwise
        contacts.insert("Daniel", "164-6743");
    
        match contacts.get(&"Ashley") {
            Some(&number) => println!("Calling Ashley: {}", call(number)),
            _ => println!("Don't have Ashley's number."),
        }
    
        contacts.remove(&"Ashley"); 
    
        // `HashMap::iter()` returns an iterator that yields 
        // (&'a key, &'a value) pairs in arbitrary order.
        for (contact, &number) in contacts.iter() {
            println!("Calling {}: {}", contact, call(number)); 
        }
    }
    

For more information on how hashing and hash maps (sometimes called hash
tables) work, have a look at [ Hash Table Wikipedia
](https://en.wikipedia.org/wiki/Hash_table)

[ __ ](../std/panic.html "Previous chapter") [ __
](../std/hash/alt_key_types.html "Next chapter")

[ __ ](../std/panic.html "Previous chapter") [ __
](../std/hash/alt_key_types.html "Next chapter")