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

#  Alternate/custom key types

Any type that implements the ` Eq ` and ` Hash ` traits can be a key in `
HashMap ` . This includes:

  * ` bool ` (though not very useful since there is only two possible keys) 
  * ` int ` , ` uint ` , and all variations thereof 
  * ` String ` and ` &str ` (protip: you can have a ` HashMap ` keyed by ` String ` and call ` .get() ` with an ` &str ` ) 

Note that ` f32 ` and ` f64 ` do _not_ implement ` Hash ` , likely because [
floating-point precision errors
](https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems) would make
using them as hashmap keys horribly error-prone.

All collection classes implement ` Eq ` and ` Hash ` if their contained type
also respectively implements ` Eq ` and ` Hash ` . For example, ` Vec<T> `
will implement ` Hash ` if ` T ` implements ` Hash ` .

You can easily implement ` Eq ` and ` Hash ` for a custom type with just one
line: ` #[derive(PartialEq, Eq, Hash)] `

The compiler will do the rest. If you want more control over the details, you
can implement ` Eq ` and/or ` Hash ` yourself. This guide will not cover the
specifics of implementing ` Hash ` .

To play around with using a ` struct ` in ` HashMap ` , let's try making a
very simple user logon system:

    
    
    use std::collections::HashMap;
    
    // Eq requires that you derive PartialEq on the type.
    #[derive(PartialEq, Eq, Hash)]
    struct Account<'a>{
        username: &'a str,
        password: &'a str,
    }
    
    struct AccountInfo<'a>{
        name: &'a str,
        email: &'a str,
    }
    
    type Accounts<'a> = HashMap<Account<'a>, AccountInfo<'a>>;
    
    fn try_logon<'a>(accounts: &Accounts<'a>,
            username: &'a str, password: &'a str){
        println!("Username: {}", username);
        println!("Password: {}", password);
        println!("Attempting logon...");
    
        let logon = Account {
            username,
            password,
        };
    
        match accounts.get(&logon) {
            Some(account_