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
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:

  1. use std::collections::HashMap;
  2. // Eq requires that you derive PartialEq on the type.
  3. #[derive(PartialEq, Eq, Hash)]
  4. struct Account<'a>{
  5. username: &'a str,
  6. password: &'a str,
  7. }
  8. struct AccountInfo<'a>{
  9. name: &'a str,
  10. email: &'a str,
  11. }
  12. type Accounts<'a> = HashMap<Account<'a>, AccountInfo<'a>>;
  13. fn try_logon<'a>(accounts: &Accounts<'a>,
  14. username: &'a str, password: &'a str){
  15. println!("Username: {}", username);
  16. println!("Password: {}", password);
  17. println!("Attempting logon...");
  18. let logon = Account {
  19. username: username,
  20. password: password,
  21. };
  22. match accounts.get(&logon) {
  23. Some(account_info) => {
  24. println!("Successful logon!");
  25. println!("Name: {}", account_info.name);
  26. println!("Email: {}", account_info.email);
  27. },
  28. _ => println!("Login failed!"),
  29. }
  30. }
  31. fn main(){
  32. let mut accounts: Accounts = HashMap::new();
  33. let account = Account {
  34. username: "j.everyman",
  35. password: "password123",
  36. };
  37. let account_info = AccountInfo {
  38. name: "John Everyman",
  39. email: "j.everyman@email.com",
  40. };
  41. accounts.insert(account, account_info);
  42. try_logon(&accounts, "j.everyman", "psasword123");
  43. try_logon(&accounts, "j.everyman", "password123");
  44. }