备选/自定义键类型

任何实现了 EqHash 特性的类型都可以作为 HashMap 的键。 这包括:

  • bool (虽然不是很实用,因为只有两个可能的键)
  • intuint 及其所有变体
  • String&str (提示:你可以使用 String 作为键的 HashMap,并使用 &str 调用 .get())

请注意,f32f64 没有 实现 Hash,这很可能是因为 浮点精度误差 会使得它们作为哈希映射键时非常容易出错。

如果它们的包含类型也分别实现了 EqHash,那么所有的集合类都将实现 EqHash。 例如,如果 T 实现了 Hash,则 Vec<T> 将实现 Hash

你可以使用一行代码轻松地为一个自定义类型实现 EqHash#[derive(PartialEq, Eq, Hash)]

编译器将完成其余的工作。 如果你想更精细地控制细节,你可以自己实现 Eq 和/或 Hash。 本指南不涵盖实现 Hash 的具体细节。

为了尝试在 HashMap 中使用 struct,让我们尝试创建一个非常简单的用户登录系统。

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_info) => {
            println!("Successful logon!");
            println!("Name: {}", account_info.name);
            println!("Email: {}", account_info.email);
        },
        _ => println!("Login failed!"),
    }
}

fn main(){
    let mut accounts: Accounts = HashMap::new();

    let account = Account {
        username: "j.everyman",
        password: "password123",
    };

    let account_info = AccountInfo {
        name: "John Everyman",
        email: "j.everyman@email.com",
    };

    accounts.insert(account, account_info);

    try_logon(&accounts, "j.everyman", "psasword123");

    try_logon(&accounts, "j.everyman", "password123");
}