The Rust Book: HashMap Challenge I'm working my way through the Rust Book:

Ch.8 Challenge:
Using a hash map and vectors, create a text interface to allow a user to add employee names to a department in the 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.

use std::collections::HashMap;
#[derive(Debug)]
enum Command {
    Add(String, String),
    List,
    Unknown
}

fn command_filter(input: String) -&gt; Command {
    let mut command_string = input.split_whitespace();
    match command_string.next() {
        Some(&quot;Add&quot;) =&gt; {
            match (command_string.next(), command_string.last()){
                (Some(name), Some(dept)) =&gt; Command::Add(name.to_string(), dept.to_string()),
                _ =&gt; Command::Unknown
            }
        },
        Some(&quot;List&quot;) =&gt; Command::List,
        _ =&gt; Command::Unknown
    }
}

fn process_command(command: Command, employee_map: &amp;mut HashMap&lt;String, String&gt;) {
    match command {
        Command::Add(name, dept) =&gt; {
            employee_map.insert(name, dept);
            ()
            },
        Command::List =&gt; {
            for (name, dept) in employee_map {
                println!(&quot;Name: {} Dept: {}&quot;, name, dept);
            };
        },
        Command::Unknown =&gt; println!(&quot;Unknown command!&quot;),
    };
}

fn main() {
    let mut employee: HashMap&lt;String, String&gt; = HashMap::new();
    let command = command_filter(String::from(&quot;Add John to Eng&quot;));
    process_command(command, &amp;mut employee);

    let command = command_filter(String::from(&quot;Add Devon to Bio&quot;));
    process_command(command, &amp;mut employee);

    let command = command_filter(String::from(&quot;List&quot;));    
    process_command(command, &amp;mut employee);    
}

As with most devs starting to learn Rust I am finding difficulty with borrowing. I do not come from a C/C++ background and so I'm also finding pointers versus actual object a little tricky too.
Some specific thoughts:

I am suspicious that match statements inside match statements is not good practice.
The Command::Add branch in process_command feels a little hacky with the () return. Edit: After thinking more about this problem, I think if I wrapped the branch responses in Ok() then I could return a Result from that function.

Do you have any feedback?
Hide hashmap implementation
The code can be improved by removing the requirement for the user to operate on a hashmap or to even know that a hashmap is used.
How? This should be hidden from the user by an &quot;interface&quot;.
Why?
This is a good coding practice as it would allow the underlying structure/code to change (for example, the hashmap could be swapped for a db) but there would be no API changes to the interface.
I have implemented this change and the changes suggested by @Shepmaster here: github