我有以下示例代码,这是其他编程语言中事件驱动的API的标准基础,但是在Rust中,借位检查器会以“一次不能多次借用p1可变”来阻止它:

struct Pen {
    color_cmyk: u32,
    ink: usize,
}

impl Pen {
    pub fn new() -> Pen {
        Pen {
            color_cmyk: 0x80800000,
            ink: 20000,
        }
    }

    pub fn write(&mut self, text: &str) -> bool {
        if self.ink < text.len() {
            return false;
        }

        self.ink -= text.len();
        true
    }
}

fn main() {
    println!("Hello, world !");

    let mut p1 = Pen::new();
    p1.write("Hello");
    println!("ink: {}, color: {}", p1.ink, p1.color_cmyk);

    let mut cb = |text| if p1.write(text) {
        println!("{}", text);
    } else {
        println!("Out of ink !");
    };

    let mut cb2 = |text| {
        p1.write(text);
        p1.ink
    };

    cb("Hello");
    cb("World");
    println!("{}", cb2("Hello"));
}

error[E0499]: cannot borrow `p1` as mutable more than once at a time
  --> src/main.rs:37:23
   |
31 |         let mut cb = |text| if p1.write(text) {
   |                      ------    -- previous borrow occurs due to use of `p1` in closure
   |                      |
   |                      first mutable borrow occurs here
...
37 |         let mut cb2 = |text| {
   |                       ^^^^^^ second mutable borrow occurs here
38 |             p1.write(text);
   |             -- borrow occurs due to use of `p1` in closure
...
45 |     }
   |     - first borrow ends here

例如,该代码可用于实现对窗口的两个回调:一个用于处理键盘事件,另一个用于处理鼠标事件,两者均会更新窗口状态(例如:更改颜色,关闭窗口等)。

我知道这个问题会出现在Stack Overflow和其他论坛的其他地方,但是总的来说,答案集中于描述问题的原因,很少提出完整的一般解决方案:
  • Cannot borrow `x` as mutable more than once at a time
  • How to bypass “cannot borrow as mutable more than once”?
  • Cannot borrow as mutable more than once at a time
  • Passing mutable context into callbacks
  • Creating a callback system using closures
  • Execute callbacks like as mutable borrowing from cycle
  • Callback to mutable self
  • 最佳答案

    一种方法是使用RefCell,它允许您仅使用&Pen而不是&mut Pen进行更改,而这需要将借阅检查推进到运行时。非常便宜:没有分配,只有一个标志测试。
    主要缺点是违反规则会导致运行时出现 panic 。一个有用的经验法则是,不要借用超过必要的时间(将它们视为“单线程互斥体”)。

    use std::cell::RefCell;
    
    fn main() {
        println!("Hello, world !");
    
        let p1 = RefCell::new(Pen::new());
        {
            let mut rp1 = p1.borrow_mut();
            rp1.write("Hello");
            println!("ink: {}, color: {}", rp1.ink, rp1.color_cmyk);
        }
    
        let cb = |text| {
            if p1.borrow_mut().write(text) {
                println!("{}", text);
            }
            else {
                println!("Out of ink !");
            }
        };
    
        let cb2 = |text| {
            let mut rp1 = p1.borrow_mut();
            rp1.write(text);
            rp1.ink
        };
    
        cb("Hello");
        cb("World");
        println!("{}", cb2("Hello"));
    }
    

    另一种方法是设置回调系统,以将要修改的对象作为参数传入。权衡之后,您的回调系统需要知道此状态。
    fn main() {
        println!("Hello, world !");
    
        let mut p1 = Pen::new();
        p1.write("Hello");
        println!("ink: {}, color: {}", p1.ink, p1.color_cmyk);
    
        let cb = |p1: &mut Pen, text| if p1.write(text) {
            println!("{}", text);
        } else {
            println!("Out of ink !");
        };
    
        let cb2 = |p1: &mut Pen, text| {
            p1.write(text);
            p1.ink
        };
    
        cb(&mut p1, "Hello");
        cb(&mut p1, "World");
        println!("{}", cb2(&mut p1, "Hello"));
    }
    

    09-25 18:38