rust - 如何在 Rust 中创建一个 HashMap,其深度可以是映射值的 4 到 5 倍?

标签 rust hashmap

我需要创建一个 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/

相关文章:

rust - 如何调试外部库中的崩溃

string - 如何从声明性宏返回一个新字符串?

java - 从 ArrayList Java 中的 HashMap 键中检索所有值

loops - 如何在Rust中的for循环中获取当前元素的索引?

rust - 有没有办法对多个语句使用 cfg(feature) 检查?

c++ - 显示 HashMap 值 C++

java - 为什么HashMap使用长度减1来计算索引?

java - 奇怪的 HashMap.put() 行为

rust - 为什么 Rust 不允许局部常量的类型推断?

java - ”HashMap“中的”volatile“关于”modCount“