【发布时间】:2018-03-13 07:34:20
【问题描述】:
我想创建一个带有嵌套结构的HashMap,就像这个复杂的例子:
{
type: boy
name: Phineas
father:
type: man
name: Lawrence
}
在 Rust 中,这将是:
use std::collections::HashMap;
let mut lawrence = HashMap::new();
lawrence.insert("type", "man");
lawrence.insert("name", "Lawrence");
let mut phineas = HashMap::new();
phineas.insert("type", "boy");
phineas.insert("name", "Phineas");
phineas.insert("father", lawrence);
但HashMap 的值似乎只能是字符串;如果我尝试编译,我会得到:
expected &str, found struct `std::collections::HashMap`
我查看了文档,但找不到构建类似数据结构的简单解决方案。
【问题讨论】:
-
phineas.insert("father", lawrence);你的意思是“劳伦斯”吗?否则你的问题是,你不能将两种不同的类型插入到同一个哈希图中