hashmap - 不能将变量借用为可变的,因为在构建自引用 HashMap 时它也被借用为不可变的

标签 hashmap rust borrow-checker

我正在尝试构建一个自引用 HashMap:

use std::collections::HashMap;

struct Node<'a> {
    byte: u8,
    map: HashMap<i32, &'a Node<'a>>,
}

fn main() {
    let mut network = HashMap::<u32, Node>::new();

    network.insert(0, Node { byte: 0, map: HashMap::<i32, &Node>::new() });
    network.insert(1, Node { byte: 1, map: HashMap::<i32, &Node>::new() });

    let zeroeth_node = network.get(&0).unwrap();
    let mut first_node = network.get_mut(&1).unwrap();

    first_node.map.insert(-1, zeroeth_node);
}

我遇到了借用检查器错误,但我不明白它的来源——是我更新 HashMap 的方法有问题,还是我的自引用用法是吗?

错误:

<anon>:15:26: 15:33 error: cannot borrow `network` as mutable because it is also borrowed as immutable [E0502]
<anon>:15     let mut first_node = network.get_mut(&1).unwrap();
                                   ^~~~~~~
<anon>:14:24: 14:31 note: previous borrow of `network` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `network` until the borrow ends
<anon>:14     let zeroeth_node = network.get(&0).unwrap();
                                 ^~~~~~~
<anon>:18:2: 18:2 note: previous borrow ends here
<anon>:8 fn main() {
...
<anon>:18 }
          ^

最佳答案

回答

这些类型的结构很难用 Rust 制作。您的示例中缺少的主要内容是使用 RefCell这允许共享引用。 RefCell 将 Rust 的借用检查从编译时转移到运行时,从而允许您传递内存位置。但是,不要开始到处使用 RefCell,因为它只适用于这种情况,RefCell 会导致您的程序 panic! 如果你试图在它已经被可变地借用时可变地借用它。这仅适用于在 network 中创建的 Node;您将无法创建纯粹存在于单个 Node 内部的 Node

Solution

use std::collections::HashMap;
use std::cell::RefCell;
#[derive(Debug)]
struct Node<'a> {
    byte: u8,
    map: HashMap<i32, &'a RefCell<Node<'a>>>,
}

fn main() {
    let mut network = HashMap::new();

    network.insert(0, RefCell::new(Node { byte: 0, map: HashMap::new() }));
    network.insert(1, RefCell::new(Node { byte: 1, map: HashMap::new() }));

    let zero_node = network.get(&0).unwrap();
    zero_node.borrow_mut().byte = 2;

    let first_node = network.get(&1).unwrap();
    first_node.borrow_mut().map.insert(-1, zero_node);

    println!("{:#?}", network);
}

关于hashmap - 不能将变量借用为可变的,因为在构建自引用 HashMap 时它也被借用为不可变的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35524499/

相关文章:

java - 优化数据结构,例如 Map<String, Map<String, Map<String, Position>>>

rust - Rust:如何与String类型匹配? [复制]

mongodb - 如何将 Rust Mongo 驱动的 BSON 类型转换为 ObjectId?

rust - 我可以在 Rust 中将不可变借用标记为独占吗?

rust - 有条件地修改 Vec 的可选元素的最惯用的 Rust 方法是什么?

rust - 错误[E0502] : cannot borrow `vector` as immutable because it is also borrowed as mutable

c++ - hash_map/unordered_map 中的项目顺序是否稳定?

java - HashMap和HashSet根据执行顺序不同的执行时间?

rust - 匹配后如何修改值?

java - 从 Hashmap 的所有值中获取所有嵌套项