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?