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
한마디로 'match'대신'if let'을 사용하는 것이 좋습니다. ['& String' 대신에'& str'을 수락하십시오.] (https://stackoverflow.com/q/40006219/155423). 내 직감은 당신이 정말로 [entry API] (https://stackoverflow.com/q/28512394/155423)를 원할 것이라고 말합니다. – Shepmaster