Modify struct values in HashMap errors

I am getting those two errors when I tried to change the values in HashMap, I tried different ways, none worked. Not sure how to fix it.

error[E0594]: cannot assign to `p1.name` which is behind a `&` reference
  --> src/main.rs:73:5
   |
73 |     p1.name = "Yujio".to_string();
   |     ^^^^^^^ cannot assign

error[E0596]: cannot borrow `p1.jobs` as mutable, as it is behind a `&` reference
  --> src/main.rs:74:5
   |
74 |     p1.jobs.push("CEO".to_string());
   |     ^^^^^^^ cannot borrow as mutable
use std::collections::HashMap;

struct Person {
    name: String,
    jobs: Vec<String>,
}

fn get_person_map() -> HashMap<u32, Person> {
    let mut h = HashMap::new();
    h.insert(1, 
            Person { 
                name: "Mikya".to_string(),
                jobs: vec!["manager".to_string(), "VP".to_string()],
            });
    return h;
}

fn main() {
    let mut h = get_person_map();
    let mut p1 = &mut h.get(&1).unwrap();

    p1.name = "Yujio".to_string();
    p1.jobs.push("CEO".to_string());
}

You probably want h.get_mut, not &mut h.get - documentation.

@Cerber-Ursi , that worked, thank you!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.