Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vector 如何扩展Vec<;字符串>;作为HashMap值?_Vector_Hashmap_Rust - Fatal编程技术网

Vector 如何扩展Vec<;字符串>;作为HashMap值?

Vector 如何扩展Vec<;字符串>;作为HashMap值?,vector,hashmap,rust,Vector,Hashmap,Rust,我有一个HashMap。我不知道如何通过增加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

我有一个
HashMap
。我不知道如何通过增加
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()]);
    }
}
fn添加员工(mut数据:HashMap)->HashMap{
环路{
println!(“请输入您希望管理的员工的姓名。”);
让mut employee=String::new();
io::stdin().read_行(&mut employee)。预期(
“读取行失败”,
);
让employee=employee.trim();
让mut department=String::new();
println!(“请输入要将员工添加到的部门名称。”);
io::stdin().read_行(&mut部门)。预期(
“读取行失败”,
);
let department=department.trim();
data.entry(department.to_string())
.extend(vec![employee.to_string()]))
.或_insert(vec![employee.to_string()]);
}
}
但它却给出了错误

错误:找不到类型为`std::collections::hash_map::Entry`的名为`extend`的方法使用以下代码:

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())); },
}
  • 如果键存在,则获取该值的可变引用并添加新字符串
  • 否则,创建一个新向量并将其插入hashmap

所有这些信息都在.

中。在进一步考虑入口API之后,我提出了以下解决方案:

let val = data.entry(department.to_string())
                .or_insert(vec!());

        val.extend(vec!(employee.to_string()));

不提供扩展,但提供Vec。如果条目中存在值,则需要对其进行操作。请查看并重试。使用此
匹配方法与我发布的答案是否存在性能差异?我认为是相同的。做一些基准测试来确定。但是,您必须像我一样将
extend
替换为
push
。它应该始终写为
vec![]
保持惯用。更好的方法是:
数据.entry(department.to_string())或_insert(vec![])。push(employee.to_string())@Boiethios--谢谢。我已经试过了,但是在
之前调用
推送
或插入
,没有成功。我不知道为什么我没有意识到顺序应该和你写的一样。当然,我没有注意到方法的交换;)