gpt4 book ai didi

rust - 实现树数据结构

转载 作者:行者123 更新时间:2023-11-29 08:16:22 25 4
gpt4 key购买 nike

我想实现一个树数据结构。我有一个 Node 结构并希望它保存对子 Node 的引用。我试过:

use std::collections::*;

#[derive(Debug)]
struct Node {
value: String,
children: HashMap<String, Node>,
}


impl Node {
fn new(value: String) -> Self {
Node {
value: value,
children: HashMap::new(),
}
}

fn add_child(&mut self, key: String, value: String) -> &mut Node {
let mut node = Node::new(value);
self.children.insert(key, node);
&mut node
}
}


fn main() {
let mut root_node = Node::new("root".to_string());
root_node.add_child("child_1_1".to_string(), "child_1_1_value".to_string());
}

此代码无法编译:

error: `node` does not live long enough
--> src/main.rs:22:10
|
22 | &mut node
| ^^^^ does not live long enough
23 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the anonymous lifetime #1 defined on the body at 19:67...
--> src/main.rs:19:68
|
19 | fn add_child(&mut self, key: String, value: String) -> &mut Node {
| ____________________________________________________________________^ starting here...
20 | | let mut node = Node::new(value);
21 | | self.children.insert(key, node);
22 | | &mut node
23 | | }
| |___^ ...ending here

error[E0382]: use of moved value: `node`
--> src/main.rs:22:10
|
21 | self.children.insert(key, node);
| ---- value moved here
22 | &mut node
| ^^^^ value used here after move
|
= note: move occurs because `node` has type `Node`, which does not implement the `Copy` trait

我该如何实现?

最佳答案

在这种情况下,实际上有必要查看编译器输出中的第二个错误消息:

error[E0382]: use of moved value: `node`
--> src/main.rs:22:10
|
21 | self.children.insert(key, node);
| ---- value moved here
22 | &mut node
| ^^^^ value used here after move
|
= note: move occurs because `node` has type `Node`, which does not implement the `Copy` trait

变量 node 在第 21 行被移动 hashmap。之后你不能再使用它了!在 Rust 中,我们有移动语义,这意味着一切都默认移动,而不是默认克隆(C++)或默认引用(Java)。您想要返回对 Node 对象的引用 inside HashMap !

一个简单的方法是插入 node,就像您已经在做的那样,然后从 hashmap 中获取值:

let mut node = Node::new(value);
self.children.insert(key.clone(), node);
self.children.get_mut(key).unwrap()

这应该清楚该函数的实际作用。但是,这段代码有一些缺点:首先,我们必须克隆 key(我们需要它来进行插入和查询),其次,hashmap 需要计算 key 的哈希值两次,这不是很方便高效的。

幸运的是,Rust 的 HashMap 有一个很好的 entry()-API .我们可以这样改变函数:

self.children.entry(key).or_insert_with(|| Node::new(value))

这是 add_child() 的全部内容!然而,现在我们注意到……我们还没有真正考虑过如果 HashMap 已经包含与给定键关联的值会发生什么!在上面的代码中,旧值被保留并返回。如果你想做其他事情(例如替换值),你可以在 Entry 对象上使用 match :

let node = Node::new(value);
match self.children.entry(key) {
Entry::Occupied(e) => {
// Maybe you want to panic!() here... but we will just
// replace the value:
e.insert(node); // discarding old value...
e.get_mut()
}
Entry::Vacant(e) => insert(node),
}

关于rust - 实现树数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43170117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com