简体   繁体   English

您如何使从HashMap :: get返回的Some的值可变?

[英]How do you make the value of a Some returned from HashMap::get mutable?

I can't figure out how to modify the value returned by Some : 我不知道如何修改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 => {}
    }
}

The compiler complains: 编译器抱怨:

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

使用get_mut()代替get()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM