Rust - 如何将数据添加到三重嵌套的 HashMap
Rust - How to add data to a triply nested 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
}
我的伪代码大致是这样的:
- 每个 json 个文件
- 将名称分成几部分,按“-”
- 对于 json 文件中的每个元素(键:值)
- 将其作为出现次数添加到result[part][key][value]中
因此,结果变量的类型将是 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 中写入 Maps 会更容易 :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;
}
我正在尝试拆分一堆 (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
}
我的伪代码大致是这样的:
- 每个 json 个文件
- 将名称分成几部分,按“-”
- 对于 json 文件中的每个元素(键:值)
- 将其作为出现次数添加到result[part][key][value]中
因此,结果变量的类型将是 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 中写入 Maps 会更容易 :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;
}