我有一个HashMap<String, Vec<String>>
。我无法弄清楚如何通过增大来更新值Vec
。我认为以下方法会起作用:
fn add_employee(mut data: HashMap<String, Vec<String>>) -> HashMap<String, Vec<String>> {
loop {
println!("Please enter the name of the employee you would like to manage.");
let mut employee = String::new();
io::stdin().read_line(&mut employee).expect(
"Failed to read line",
);
let employee = employee.trim();
let mut department = String::new();
println!("Please enter the name of the department you would like to add the employee to.");
io::stdin().read_line(&mut department).expect(
"Failed to read line",
);
let department = department.trim();
data.entry(department.to_string())
.extend(vec![employee.to_string()])
.or_insert(vec![employee.to_string()]);
}
}
但是它却给出了错误
error: no method named `extend` found for type `std::collections::hash_map::Entry<'_, std::string::String, std::vec::Vec<std::string::String>>` in the current scope
--> src/main.rs:27:14
|
27 | .extend(vec![employee.to_string()])
| ^^^^^^
使用此代码:
use std::collections::hash_map::Entry;
match data.entry(department.to_string()) {
Entry::Occupied(mut entry) => { entry.get_mut().push(employee.to_string()); },
Entry::Vacant(entry) => { entry.insert(vec!(employee.to_string())); },
}
所有这些信息都在文档中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句