How do I add an element to the value of a hash map that pairs strings with vectors of strings?

﹥>﹥吖頭↗ 提交于 2019-12-24 18:50:31

问题


Like many new Rustaceans, I've been working my way through the Rust Book. I'm reading through the chapter on collections, and I'm stuck on one of the exercises. It reads:

Using a hash map and vectors, create a text interface to allow a user to add employee names to a department in a company. For example, “Add Sally to Engineering” or “Add Amir to Sales.” Then let the user retrieve a list of all people in a department or all people in the company by department, sorted alphabetically.

Here is my code so far:

use std::collections::hash_map::OccupiedEntry;
use std::collections::hash_map::VacantEntry;
use std::collections::HashMap;
use std::io;

fn main() {
    println!("Welcome to the employee database text interface.\nHow can I help you?");

    let mut command = String::new();

    io::stdin()
        .read_line(&mut command)
        .expect("Failed to read line.");

    command = command.to_lowercase();

    let commands = command.split_whitespace().collect::<Vec<&str>>();

    let mut department = commands[3];
    let mut name = commands[1];

    let mut employees = HashMap::new();

    if commands[0] == "add" {
        match employees.entry(department) {
            VacantEntry(entry) => entry.entry(department).or_insert(vec![name]),
            OccupiedEntry(entry) => entry.get_mut().push(name),
        }
    }
}

The compiler returns the following error:

error[E0532]: expected tuple struct/variant, found struct `VacantEntry`
  --> src/main.rs:26:13
   |
26 |             VacantEntry(entry) => entry.entry(department).or_insert(vec![name]),
   |             ^^^^^^^^^^^ did you mean `VacantEntry { /* fields */ }`?

error[E0532]: expected tuple struct/variant, found struct `OccupiedEntry`
  --> src/main.rs:27:13
   |
27 |             OccupiedEntry(entry) => entry.get_mut().push(name),
   |             ^^^^^^^^^^^^^ did you mean `OccupiedEntry { /* fields */ }`?

I'm not exactly sure what I'm doing wrong. What do these errors mean, and what can I do to fix them and get my code to compile?


回答1:


You need to understand the difference between an enum variant and the type of that variant. The variants of Entry are Vacant, and Occupied. You need to match against those variants, not their types.

One way to fix your code would be like this:

use std::collections::hash_map::Entry::{Vacant, Occupied};
match employees.entry(department) {
    Vacant(entry) => { entry.insert(vec![name]); },
    Occupied(mut entry) => { entry.get_mut().push(name); },
}

But a much simpler solution would to use the return value of or_insert, which is a reference to the vector, and push onto it.

employees
    .entry(department)
    .or_insert(Vec::new())
    .push(name);


来源:https://stackoverflow.com/questions/53368852/how-do-i-add-an-element-to-the-value-of-a-hash-map-that-pairs-strings-with-vecto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!