Data structures 如何在结构中插入hashmap?
我有几个状态,我想存储在各自的hashmaps中。看起来像这样的东西:Data structures 如何在结构中插入hashmap?,data-structures,rust,hashmap,Data Structures,Rust,Hashmap,我有几个状态,我想存储在各自的hashmaps中。看起来像这样的东西: let mut directory_hash: HashMap<String, String> = HashMap::new(); let mut component_hash: HashMap<String, String> = HashMap::new(); let mut directive_hash: HashMap<String, String> = HashMap::new()
let mut directory_hash: HashMap<String, String> = HashMap::new();
let mut component_hash: HashMap<String, String> = HashMap::new();
let mut directive_hash: HashMap<String, String> = HashMap::new();
现在让我们假设我想插入一个嵌套的hashmap。我该怎么做
下面是几个不起作用的示例的一个片段:
无法将'std::collections::HashMap'索引中的数据作为可变数据借用
不能像易变的那样借钱
帮助:修改索引内容需要trait`IndexMut',但对于`std::collections::HashMap`rustc(E0596)没有实现
使用不稳定的库功能“entry\u insert”
注:有关更多信息,请参阅第65225期
我已经看过了,但我不确定这是否是应该采取的方法pub struct State {
pub directory_hash: HashMap<String, String>,
pub component_hash: HashMap<String, String>,
pub module_hash: HashMap<String, String>,
}
impl State {
pub fn new() -> State {
let directory_hash: HashMap<String, String> = HashMap::new();
let component_hash: HashMap<String, String> = HashMap::new();
let module_hash: HashMap<String, String> = HashMap::new();
State {
directory_hash,
component_hash,
module_hash,
}
}
}
let app_state = state::app_state::State::new();
let mut dir_hash = app_state.directory_hash;
dir_hash.insert(String::from("1"), String::from("first value!"));
pub结构状态{
发布目录\u哈希:HashMap,
发布组件_散列:HashMap,
发布模块_散列:HashMap,
}
impl状态{
pub fn new()->State{
让目录_散列:HashMap=HashMap::new();
让组件散列:HashMap=HashMap::new();
让模块_散列:HashMap=HashMap::new();
陈述{
目录\u散列,
组件_散列,
模块_散列,
}
}
}
让app_state=state::app_state::state::new();
让mut dir_hash=app_state.directory_hash;
插入(String::from(“1”),String::from(“第一个值”);
如错误消息所示。用于获取选项
:
()
let mut app_state = state::app_state::init_app_state();
let mut dir_hash = &mut app_state["directory_hash"];
dir_hash.insert(String::from("1"), String::from("first entry"));
let mut app_state = state::app_state::init_app_state();
let mut dir_hash = &mut app_state.entry("directory_hash");
dir_hash.insert(String::from("1"), String::from("first entry"));
pub struct State {
pub directory_hash: HashMap<String, String>,
pub component_hash: HashMap<String, String>,
pub module_hash: HashMap<String, String>,
}
impl State {
pub fn new() -> State {
let directory_hash: HashMap<String, String> = HashMap::new();
let component_hash: HashMap<String, String> = HashMap::new();
let module_hash: HashMap<String, String> = HashMap::new();
State {
directory_hash,
component_hash,
module_hash,
}
}
}
let app_state = state::app_state::State::new();
let mut dir_hash = app_state.directory_hash;
dir_hash.insert(String::from("1"), String::from("first value!"));
let mut app_state = state::app_state::init_app_state();
let dir_hash = app_state.get_mut("directory_hash").unwrap();
dir_hash.insert(String::from("1"), String::from("first entry"));