collections - Rust - 如何将数据添加到三重嵌套 HashMap

标签 collections rust hashmap

我正在尝试拆分一堆(50+)json 文件,为此我想根据文件名的元素按频率对它们的值进行分类。我不仅在这样做,而且我作为一个 Rust 新手也在这样做,因为例如,在 Python 中这样做不够有趣。

为了便于解释,让我们想象一下这样的文件:

a-b-c-d.json
{
    "var":"foo",
    "array": [
        'one',
        'two'
    ]
}
b-c-d-f.json
{
    "var":"bar",
    "array": [
        'one',
        'three'
    ]
}

处理这两个文件后,我想要最终得到的数据结构是这样的(请注意,这很可能是我的第一个错误):

{
  'a' : {
    'var' : { 
      'foo': 1
    }, 'array' : {
      'one': 1, 'two': 1 
    }
  },
  'b' : {
    'var' : { 
      'foo': 1,
      'bar': 1
    }, 'array' : {
      'one': 2, 'two': 1, 'three': 1
    }
  }
(also 'c', 'd' and 'f') - I hope the intention is clear with this small example
}

我的伪代码或多或少是这样的:

  1. 对于每个 json 文件
  2. 用“-”将名称分成几部分
  3. 对于 json 文件中的每个元素(键:值)
  4. 将其作为出现次数添加到结果[部分][键][值]

因此,结果变量的类型将是 HashMap<String, HashMap<String, HashMap<String, i8>>>

我收到错误的(实际)代码是:

type ComponentValueElement = HashMap<String, i8>;
type ComponentElement = HashMap<String, ComponentValueElement>;
type ComponentMap = HashMap<String, ComponentElement>;
(...)
fn increase_ocurrence_of_element_in_configuration(component: &String, element: &String, value: &String, final_component_map: &mut ComponentMap) {
    final_component_map.entry(component.into()).or_insert( HashMap::new() );
    final_component_map[component].entry(element.into()).or_insert(HashMap::new());
    final_component_map[component][element].entry(value.into()).or_insert(0);

    final_component_map[component][element][value] += 1;
}

错误是:

<final_component_map[component]>.entry(element.into()).or_insert(HashMap::new());

cannot borrow data in an index of `std::collections::HashMap<std::string::String, std::collections::HashMap<std::string::String, std::collections::HashMap<std::string::String, i8>>>` as mutable

cannot borrow as mutable

help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<std::string::String, std::collections::HashMap<std::string::String, std::collections::HashMap<std::string::String, i8>>>`

<final_component_map[component][element]>.entry(value.into()).or_insert(0);

cannot borrow data in an index of `std::collections::HashMap<std::string::String, std::collections::HashMap<std::string::String, i8>>` as mutable

cannot borrow as mutable

help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<std::string::String, std::collections::HashMap<std::string::String, i8>>`

<final_component_map[component][element][value]> += 1;

cannot assign to data in an index of `std::collections::HashMap<std::string::String, i8>`

cannot assign

help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<std::string::String, i8>`

我本以为在 Rust 中写入 Map 会更容易:D 我到底需要做什么才能让我美丽的嵌套 HashMap 填充值?

非常感谢!

最佳答案

Entry API 的部分目的是让您可以更改条目而无需再次查找它。您可以将这些查找链接在一起,如下所示:

fn increase_ocurrence_of_element_in_configuration(
    component: &str,
    element: &str,
    value: &str,
    final_component_map: &mut ComponentMap,
) {
    *final_component_map
        .entry(component.into())
        .or_default()
        .entry(element.into())
        .or_default()
        .entry(value.into())
        .or_default() += 1;
}

关于collections - Rust - 如何将数据添加到三重嵌套 HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66784124/

相关文章:

collections - 安装 PECL 包 : Mysql_xdevapi 时出错

string - Rust 的 `String` 和 `str` 有什么区别?

java - 如何添加到无界列表? (泛型)

java - 无法使用修饰符 "public final"访问 java.util.HashMap$Entry

java - 使用 Java8 Stream API 合并两个 hashmap 列表

java - 如何检查java集合的所有元素是否都符合某些条件?

rust - 将 GtkSourceView 与 Rust 结合使用

rust - 匹配结果类型时提早退出

java - 有没有办法提取Map的值?

java - 在JAVA中获取树形图中唯一值的所有键