Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用None访问Rust中的嵌套HashMap_Rust_Hashmap_Ownership - Fatal编程技术网

使用None访问Rust中的嵌套HashMap

使用None访问Rust中的嵌套HashMap,rust,hashmap,ownership,Rust,Hashmap,Ownership,我想创建一个类似于中的数据结构。因此,一个由数据库节点组成的树,其中包含一些与该节点相关联的数据,以及更深入的节点 不同之处在于,我希望允许子节点成为无节点,以指示节点是叶节点 所以它应该看起来像: { “1”:数据库{ { 数据:“要素1”, 孩子们:一些({ “a”:数据库{ 数据:“要素1-a”, 儿童:没有 }, “b”:数据库{ 数据:“要素1-b”, 儿童:没有 } }) } }, “2”:数据库{ { 数据:“要素2”, 儿童:没有 } } } 使用中的代码,我想出了这个[]:

我想创建一个类似于中的数据结构。因此,一个由
数据库
节点组成的树,其中包含一些与该节点相关联的数据,以及更深入的节点

不同之处在于,我希望允许
子节点
成为
无节点
,以指示节点是叶节点

所以它应该看起来像:

{
“1”:数据库{
{
数据:“要素1”,
孩子们:一些({
“a”:数据库{
数据:“要素1-a”,
儿童:没有
},
“b”:数据库{
数据:“要素1-b”,
儿童:没有
}
})
}
},
“2”:数据库{
{
数据:“要素2”,
儿童:没有
}
}
}
使用中的代码,我想出了这个[]:

#[派生(默认,调试)]
结构数据库{
儿童:选择,
数据:字符串,
}
impl数据库{
fn插入路径(&mut self,路径:&[&str])->&mut self{
让mut节点=self;
for path.iter()中的子键(&S){
如果让None=node.children{
node.children=Some(HashMap::new());
}
节点=节点
儿童
.unwrap()
.entry(子键.to_string())
。或使用(数据库::默认值)插入;
}
节点
}
}
fn main(){
让mut db=数据库{
子项:一些(HashMap::new()),
数据:“根”。到_字符串(),
};
让node=db.insert_path(&vec![“key1”、“key1.1”、“key1.1.3]”);
node.data=“myvalue”to_string();
println!(“{:?}”,db);
}
这不管用。我得到以下错误:

error[E0507]: cannot move out of `node.children` which is behind a mutable reference
  --> src/main.rs:18:20
   |
18 |               node = node
   |  ____________________^
19 | |                 .children
   | |_________________________^ move occurs because `node.children` has type `Option<HashMap<String, Database>>`, which does not implement the `Copy` trait
   |
help: consider borrowing the `Option`'s content
   |
18 |             node = node
19 |                 .children.as_ref()
   |

error[E0515]: cannot return value referencing temporary value
  --> src/main.rs:24:9
   |
18 |               node = node
   |  ____________________-
19 | |                 .children
20 | |                 .unwrap()
   | |_________________________- temporary value created here
...
24 |           node
   |           ^^^^ returns a value referencing data owned by the current function

error[E0507]:无法移出可变引用后面的'node.children'
-->src/main.rs:18:20
|
18 |节点=节点
|  ____________________^
19.儿童
||uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
|
帮助:考虑借用“选项”的内容
|
18 |节点=节点
19 |儿童。如|参考(
|
错误[E0515]:无法返回引用临时值的值
-->src/main.rs:24:9
|
18 |节点=节点
|  ____________________-
19.儿童
20 | |展开()
||
...
24 |节点
|^^^^返回一个值,该值引用当前函数拥有的数据
我很不明白为什么会这样。我认为在
node.children
上使用
unwrap()
会删除移动的值
node.children
。但是,如果不使用
unwrap()
,我看不出如何执行此操作。我怎样才能用这个使用
None
的新结构实现原始帖子的功能?这可能吗


注意:我还删减了原来的代码,以便与上面的代码更相似,更易于比较。有关游乐场链接,请参阅。

如果在
儿童
之后添加
as_mut()
,则示例将编译,即:

node = node
    .children
    .as_mut()
    .unwrap()
    .entry(subkey.to_string())
    .or_insert_with(Database::default);

选项::as_mut
选项
转换为
选项
,从而防止在
展开(
节点时移出
节点。如果在
子节点
之后添加
as_mut()
,则示例将编译,即:

node = node
    .children
    .as_mut()
    .unwrap()
    .entry(subkey.to_string())
    .or_insert_with(Database::default);
选项::as_mut
选项
转换为
选项
,从而防止在
展开()节点时移出
节点。子节点