rust - 实现树形数据结构

标签 rust borrowing

我想实现一个树形数据结构。我有一个 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)。您想要返回对 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/

相关文章:

rust - 如何复制而不是借用 i64 到 Rust 的闭包中?

rust - 如何检查两个引用变量是否借用同一个对象?

rust - 为什么在闭包内克隆数据不能防止错误 "closure may outlive the current function"?

memory-management - Rust 结构体上 getter 方法的借用问题

rust - 当输入非常清楚时,为什么借用检查器需要输出生命周期标签?

types - Rust 中这个奇怪的递归类型错误是怎么回事?

c++ - 如何将 C++ 程序链接到 Rust 程序,然后将该 Rust 程序链接回 C++ 程序? (cpp -> rust -> cpp)

macros - 如何强制类型在编译时实现特征?

logging - 如何使用 log4rs 创建自定义过滤器类型?

rust - 无法使用 cairo-rs 链接程序 : "linking with ` cc` failed"and "library not found for -lgobject-2.0"