我如何在 Rust 中创建一个 HashMap,它可以使用映射的值进行 4 到 5 倍的深度?
How can I create a HashMap in Rust that can go 4 to 5 times deep with values that are maps?
我需要创建一个 HashMap
,其中的值是 HashMap
并且可能包含也可以是 HashMap 的值,这可以达到 4 到 5 倍深。例如;
use std::collections::HashMap;
fn main() {
let entry: HashMap<String, T> = HashMap::new();
}
T 可以是包含其他 HashMap
作为值的 HashMap
,依此类推。键总是 String
,叶值可以是各种类型。我怎样才能做到这一点?
“叶值可以有多种类型” - 这对我来说听起来像是 enum
。
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();
}
我需要创建一个 HashMap
,其中的值是 HashMap
并且可能包含也可以是 HashMap 的值,这可以达到 4 到 5 倍深。例如;
use std::collections::HashMap;
fn main() {
let entry: HashMap<String, T> = HashMap::new();
}
T 可以是包含其他 HashMap
作为值的 HashMap
,依此类推。键总是 String
,叶值可以是各种类型。我怎样才能做到这一点?
“叶值可以有多种类型” - 这对我来说听起来像是 enum
。
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();
}