1

我需要在 Rust 中实现 A* 寻路算法。该算法处理特定单元格并使用parent_cell字段相互链接。最后,当找到目标单元格时,路径就是从目标单元格到起始单元格的路径。我需要捕获路径中的每个单元格Vec。我创建了一段简化的代码来反映我的问题:

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

struct AstarCell {
    parent_cell: Option<Rc<RefCell<AstarCell>>>,
}

fn main() {
    let start = Rc::new(RefCell::new(AstarCell { parent_cell: None }));
    let path_cell_1 = Rc::new(RefCell::new(AstarCell {
        parent_cell: Some(Rc::clone(&start)),
    }));
    let path_cell_2 = Rc::new(RefCell::new(AstarCell {
        parent_cell: Some(Rc::clone(&path_cell_1)),
    }));
    let destination = Rc::new(RefCell::new(AstarCell {
        parent_cell: Some(Rc::clone(&path_cell_2)),
    }));

    let mut path_list: Vec<Rc<RefCell<AstarCell>>> = Vec::new();

    let mut current_cell = destination.clone();

    while let Some(parent_cell) = current_cell.borrow().parent_cell {
        path_list.push(Rc::clone(&parent_cell));
        current_cell = Rc::clone(&parent_cell);
    }
}

这不起作用,因为借用检查器妨碍了我:

error[E0507]: cannot move out of borrowed content
  --> src/main.rs:24:35
   |
24 |     while let Some(parent_cell) = current_cell.borrow().parent_cell {
   |                    -----------    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |                    |              |
   |                    |              cannot move out of borrowed content
   |                    |              help: consider borrowing here: `&current_cell.borrow().parent_cell`
   |                    data moved here
   |
note: move occurs because `parent_cell` has type `std::rc::Rc<std::cell::RefCell<AstarCell>>`, which does not implement the `Copy` trait
  --> src/main.rs:24:20
   |
24 |     while let Some(parent_cell) = current_cell.borrow().parent_cell {
   |                    ^^^^^^^^^^^

error[E0506]: cannot assign to `current_cell` because it is borrowed
  --> src/main.rs:26:9
   |
24 |     while let Some(parent_cell) = current_cell.borrow().parent_cell {
   |                                   ---------------------           - ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `std::cell::Ref<'_, AstarCell>`
   |                                   |
   |                                   borrow of `current_cell` occurs here
   |                                   a temporary with access to the borrow is created here ...
25 |         path_list.push(Rc::clone(&parent_cell));
26 |         current_cell = Rc::clone(&parent_cell);
   |         ^^^^^^^^^^^^ assignment to borrowed `current_cell` occurs here
   |
   = note: The temporary is part of an expression at the end of a block. Consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped.

我尝试&按照编译器的建议使用借用,但感觉这不是正确的方法。

4

0 回答 0