迭代列表时,借用的RefCell持续时间不够长

问题描述 投票:3回答:1

我正在尝试实现一个链表来理解Rust中的智能指针。我定义了一个Node

use std::{cell::RefCell, rc::Rc};

struct Node {
    val: i32,
    next: Option<Rc<RefCell<Node>>>,
}

和迭代一样

fn iterate(node: Option<&Rc<RefCell<Node>>>) -> Vec<i32> {
    let mut p = node;
    let mut result = vec![];

    loop {
        if p.is_none() {
            break;
        }

        result.push(p.as_ref().unwrap().borrow().val);

        p = p.as_ref().unwrap().borrow().next.as_ref();
    }

    result
}

编译器报告错误:

error[E0716]: temporary value dropped while borrowed
  --> src/main.rs:27:13
   |
27 |         p = p.as_ref().unwrap().borrow().next.as_ref();
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^              -
   |             |                                         |
   |             |                                         temporary value is freed at the end of this statement
   |             |                                         ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `std::cell::Ref<'_, Node>`
   |             creates a temporary which is freed while still in use
   |             a temporary with access to the borrow is created here ...
   |
   = note: consider using a `let` binding to create a longer lived value

发生了什么?我们不能使用引用来迭代这种方式定义的节点吗?

linked-list rust smart-pointers
1个回答
2
投票

你需要克隆p,而不是为借用的引用分配Rc

use std::cell::RefCell;
use std::rc::Rc;

struct Node {
    val: i32,
    next: Option<Rc<RefCell<Node>>>,
}

fn iterate(node: Option<Rc<RefCell<Node>>>) -> Vec<i32> {
    let mut p = node;
    let mut result = vec![];

    loop {
        let node = match p {
            None => break,
            Some(ref n) => Rc::clone(n), // Clone the Rc
        };

        result.push(node.as_ref().borrow().val); //works because val is Copy
        p = match node.borrow().next {
            None => None,
            Some(ref next) => Some(Rc::clone(next)), //clone the Rc
        };
    }

    result
}

fn main() {
    let node = Some(Rc::new(RefCell::new(Node {
        val: 0,
        next: Some(Rc::new(RefCell::new(Node { val: 1, next: None }))),
    })));

    let result = iterate(node);
    print!("{:?}", result)
}

这是必要的,因为您尝试在需要更长使用寿命的上下文中使用寿命更短的变量。在循环迭代之后,p.as_ref().unwrap().borrow()的结果被删除(即释放,解除分配),但是你试图在下一个循环中使用它的成员(这被称为use after free,Rust的设计目标之一就是防止这种情况)。

问题是借用不拥有该对象。如果你想在下一个循环中使用next作为p,那么p必须拥有该对象。这可以通过Rc(即'引用计数')来实现,并允许单个线程中的多个所有者。

如果Node::next的定义是Option<Box<RefCell<Node>>>,如何迭代这个列表怎么办?

是的,我也对RefCell非常困惑,没有RefCell我们可以使用引用迭代列表,但是会因RefCell而失败。我甚至试图添加一个Ref矢量来保存参考,但仍然无法成功。

如果你删除RefCell,你可以像这样迭代它:

struct Node {
    val: i32,
    next: Option<Box<Node>>,
}

fn iterate(node: Option<Box<Node>>) -> Vec<i32> {
    let mut result = vec![];
    let mut next = node.as_ref().map(|n| &**n);

    while let Some(n) = next.take() {
        result.push(n.val);

        let x = n.next.as_ref().map(|n| &**n);
        next = x;
    }

    result
}

fn main() {
    let node = Some(Box::new(Node {
        val: 0,
        next: Some(Box::new(Node { val: 1, next: None })),
    }));

    let result = iterate(node);
    print!("{:?}", result)
}

也许它有可能与RefCell,但我无法解决终身问题。

© www.soinside.com 2019 - 2024. All rights reserved.