你如何使从HashMap ::返回的some的值变得可变?

时间:2017-10-30 23:24:19

标签: rust

我无法弄清楚如何修改Some返回的值:

fn add_employee(
    employees: &mut HashMap<String, Vec<String>>,
    employee_name: &String,
    department_name: &String,
) {
    match employees.get(department_name) {
        Some(members) => {
            members.push(employee_name.clone()); // what I want, but it doesn't work
        }
        None => {}
    }
}

编译器抱怨:

error[E0596]: cannot borrow immutable borrowed content `*members` as mutable
  --> src/main.rs:10:13
   |
10 |             members.push(employee_name.clone());
   |             ^^^^^^^ cannot borrow as mutable

1 个答案:

答案 0 :(得分:4)

使用get_mut()代替get()