1

Could someone explain me why does it say that *profession is a unit type while it is a vector?

use hashbrown::HashMap;

fn main() {
    let mut sphere: HashMap<String, Vec<&str>> = HashMap::new();
    sphere.insert(String::from("junior"), vec![]);
    sphere.insert(String::from("Middle"), vec![]);
    sphere.insert(String::from("Senior"), vec![]);
    loop {
        println!();
        let mut input = String::new();
        std::io::stdin()
            .read_line(&mut input)
            .expect("What the hell this doesn't work!?");
        if input.trim() == "stop" {
            break;
        }
        let splited_data: Vec<&str> = input.split(" to ").collect();
        let person = splited_data[0];
        let category = splited_data[1].to_string();
        let professions = sphere.entry(category.to_string()).or_insert(vec![]);
        *professions.push(person);
    }
}
error[E0614]: type `()` cannot be dereferenced
  --> src/lib.rs:21:9
   |
21 |         *professions.push(person);
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error
Peter Hall
  • 53,120
  • 14
  • 139
  • 204

1 Answers1

9

The * dereference operator has lower precedence than ., so this:

*professions.push(person);

is equivalent to:

*(professions.push(person));

The error you are seeing is because Vec::push returns ().

What you really want is to dereference the vector and then call push:

(*professions).push(person);

But Rust's auto-deref rules make the the explicit dereference unnecessary and you can just write:

professions.push(person);

See also: What are Rust's exact auto-dereferencing rules?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Peter Hall
  • 53,120
  • 14
  • 139
  • 204