我需要创建一个 HashMap
,其值是 HashMap
,并且可能包含也可能是 HashMap 的值,这可能会深 4 到 5 倍。例如;
use std::collections::HashMap;
fn main() {
let entry: HashMap<String, T> = HashMap::new();
}
T 可以是一个 HashMap
,其中包含其他 HashMap
作为值等。键始终是String
,叶值可以是各种类型。我怎样才能实现这个目标?
最佳答案
“叶子值可以有多种类型” - 对我来说这听起来像是一个枚举
。
use std::collections::HashMap;
enum Value {
Leaf(String), // or whatever types they can be
Nested(HashMap<String, Value>),
}
fn main() {
let entry: HashMap<String, Value> = HashMap::new();
}
关于rust - 如何在 Rust 中创建一个 HashMap,其深度可以是映射值的 4 到 5 倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65353002/