Data structures 如何在结构中插入hashmap?

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()

我有几个状态,我想存储在各自的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();
现在让我们假设我想插入一个嵌套的hashmap。我该怎么做

下面是几个不起作用的示例的一个片段:

  • 上面的错误在第二行(app_state[“directory_hash”])

    无法将'std::collections::HashMap'索引中的数据作为可变数据借用
    不能像易变的那样借钱
    帮助:修改索引内容需要trait`IndexMut',但对于`std::collections::HashMap`rustc(E0596)没有实现
    
  • 这里的错误在第3行(dir_hash.insert(…))

    使用不稳定的库功能“entry\u insert”
    注:有关更多信息,请参阅第65225期
    
    我已经看过了,但我不确定这是否是应该采取的方法

  • 如何更新这个嵌套的状态位

    我可能会将这些HashMap实例中的每一个都更改为状态结构上的属性。但我仍然很好奇上面的方法在锈迹中是如何工作的

    以下是我目前采取的方法:

    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"));