【问题标题】:HashMap<K,V>.entry() erroring with `undeclared type 'Entry'`HashMap<K,V>.entry() 错误并带有“未声明的类型‘Entry’”
【发布时间】:2021-04-30 22:44:55
【问题描述】:

当密钥存在时,我试图在HashMap&lt;String, Vec&lt;String&gt;&gt; 中为Vec&lt;String&gt; 添加一个值,并在不存在时创建一个新条目。我正在尽我所能关注这方面的文档,但仍然收到此错误:

error[E0433]: failed to resolve: use of undeclared type `Entry`

对于以下代码:

use std::collections::HashMap; 
fn main() {
    let mut dir: HashMap<String, Vec<String>> = HashMap::new();
    let group = String::from("Sales");
    let emp = String::from("Bob");
    
    match dir.entry(group) {
        Entry::Vacant(e) => e.insert(vec![emp]),
        Entry::Occupied(mut e) => e.get_mut().push(emp)
    }
    
    println!("{:?}", dir)
}

【问题讨论】:

  • use std::collections::hash_map::*;

标签: rust


【解决方案1】:

类型必须在范围内才能引用它。因此,您必须为std::collections::hash_map::Entry 添加一个use 声明,或者在每次编写时对其进行完全限定。

【讨论】:

    猜你喜欢
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    相关资源
    最近更新 更多