2017-10-30 3 views
0

私はSomeによって返された値を変更する方法を見つけ出すことはできません。HashMap :: getから返された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

1武装の 'match'の代わりに' if let'を使うのが好ましいです。 ['&String'の代わりに'&str'を受け入れます(https://stackoverflow.com/q/40006219/155423)。私の直感はあなたが本当に[エントリーAPI](https://stackoverflow.com/q/28512394/155423)を望んでいると言います。 – Shepmaster

答えて

4

利用get_mut()の代わりget()

+0

ああ、ありがとう!私はまだ何とかこれを逃したドキュメントを介して作業しています。 –

関連する問題