如何在从成员函数调用的闭包中修改self?

14

我正在尝试计算合法的国际象棋走法,但是在满足借贷检查器方面遇到了问题。我有一个实现了这些方法的结构体Chess(非重要代码由...代替):

// internal iterator over (possibly not legal) moves
fn get_moves<F>(&self, func: F)
where
    F: Fn(/* ... */),
{
    func(/* ... */); // move 1
    func(/* ... */); // move 2
    func(/* ... */); // etc...
}

fn is_legal_move(&mut self) -> bool {
    // notice this takes a mutable self. For performance
    // reasons, the move is made, legality is checked, then I
    // undo the move, so it must be mutable to be able to move pieces
    make_move(/* ... */);
    // check if legal
    undo_move(/* ... */);
    //return true if legal
}

fn get_legal_moves(&self) /* -> ... */ {
    self.get_moves(|/* ... */| {
        if self.is_legal_move(/* ... */) { // <-- error here
            // do something with legal move
        }
    })
}

get_legal_moves中我遇到了编译错误,因为我在闭包内修改了self,而此时get_moves仍然在借用self

我创建了一个简化的示例来展示我要解决的问题:

struct Tester {
    x: i8,
}

impl Tester {
    fn traverse<Func>(&mut self, mut f: Func)
    where
        Func: FnMut(),
    {
        //in real-world, this would probably iterate over something
        f();
    }
}

fn main() {
    let mut tester = Tester { x: 8 };
    tester.traverse(|| {
        tester.x += 1; //I want to be able to modify tester here
    });
    println!("{}", tester.x);
}

Playground

错误:

error[E0499]: cannot borrow `tester` as mutable more than once at a time
  --> src/main.rs:17:5
   |
17 |       tester.traverse(|| {
   |       ^      -------- -- first mutable borrow occurs here
   |       |      |
   |  _____|      first borrow later used by call
   | |
18 | |         tester.x += 1; //I want to be able to modify tester here
   | |         ------ first borrow occurs due to use of `tester` in closure
19 | |     });
   | |______^ second mutable borrow occurs here

error[E0499]: cannot borrow `tester` as mutable more than once at a time
  --> src/main.rs:17:21
   |
17 |     tester.traverse(|| {
   |     ------ -------- ^^ second mutable borrow occurs here
   |     |      |
   |     |      first borrow later used by call
   |     first mutable borrow occurs here
18 |         tester.x += 1; //I want to be able to modify tester here
   |         ------ second borrow occurs due to use of `tester` in closure

我该如何满足借用检查器以使代码能够编译?


1
另一个建议:在本地克隆状态以进行移动。这是我实现类似功能的方式。 - sellibitze
此外,我真的不知道为什么这个被踩了。这种行为开始让我在[rust]和Reddit上感到烦恼。 - sellibitze
@sellibitze 最好的回应是投票支持那些你认为经过充分研究、措辞准确、并且切题的问题(所有符合如何提问的要求)。那些不认为问题好的人会进行反对票。随着时间的推移,众人的智慧将决定问题的答案 ^_^。 - Shepmaster
1个回答

7
最简单的更改是将引用传递给闭包:
struct Tester {
    x: i8,
}

impl Tester {
    fn traverse<F>(&mut self, mut f: F)
    where
        F: FnMut(&mut Tester),
    {
        f(self);
    }
}

fn main() {
    let mut tester = Tester { x: 8 };
    tester.traverse(|z| z.x += 1);
    println!("{}", tester.x);
}

这可以防止出现多个可变引用(也称为别名),在Rust中是不允许的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接