如何在 Rust 中使用 struct 制作一种哈希映射

How to make a type of hash map with struct in rust

我是 Rust 的新手,我希望像这样在我的结构中实现我的 hashmap

#[derive(Clone, Data)]
struct CheckState { 
    cool: String,
    int_cool: i32,
    hashmap_cool: HashMap<i32, String>
}

但我一直收到错误代码error[E0277]: the trait bound `HashMap<i32, std::string::String>: Data` is not satisfied,我不明白为什么,帮助也不是很大

防锈帮助

 help: the following other types implement trait `Data`:
            &'static str
            ()
            (T0, T1)
            (T0, T1, T2)
            (T0, T1, T2, T3)
            (T0, T1, T2, T3, T4)
            (T0, T1, T2, T3, T4, T5)
            (T0,)
          and 87 others
  = note: this error originates in the derive macro `Data` (in Nightly builds, run with -Z macro-backtrace for more info)

请忽略变量名,它们在我的代码中不一样

要进行简单修复,请添加:

struct CheckState { 
    cool: String,
    int_cool: i32,
    #[data(ignore)]
    hashmap_cool: HashMap<i32, String>
}

查看 druid documentation 以获得更好的理解:

  • #[data(ignore)] makes the generated Data::same function skip comparing this field.