rust 如何解决"已变借"错误:借入错误'

7xllpg7q  于 2023-03-02  发布在  其他
关注(0)|答案(1)|浏览(187)

我对铁 rust 不是很在行
我想知道如何解决下面的错误。
如果有人能回答我的问题,我将非常感激。
thread 'main' panicked at 'already mutably borrowed: BorrowError', src/main.rs:47:22

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

// parent
struct P {
    i: Rc<RefCell<I>>,
}

impl P {
    fn new() -> P {
        let b = B {
            v: "b".to_string(),
            i: Weak::new(),
        };
        let c = C {
            v: "c".to_string(),
            i: Weak::new(),
        };
        let i = Rc::new(RefCell::new(I { b, c }));
        let ii = i.clone();
        let p = P { i };

        // init b.i
        let mut borrow_mut = RefCell::borrow_mut(&ii);
        let bb = &mut borrow_mut.b;
        bb.i = Rc::downgrade(&ii);

        // init c.i
        let cc = &mut borrow_mut.c;
        cc.i = Rc::downgrade(&ii);
        p
    }

    fn update_bv_cv(&self) {
        // update b.v
        let mut borrow_mut = RefCell::borrow_mut(&self.i);
        let b = &mut borrow_mut.b;
        b.v.push_str("=>p.update_bv_cv");

        // update c.v
        let c = &mut borrow_mut.c;
        c.v.push_str("=>p.update_bv_cv");

        // b update c.v
        let borrow = RefCell::borrow(&self.i);
        let b = &borrow.b;
        b.update_cv();
    }

    fn get_bv_cv(&self) -> (String, String) {
        let i = &self.i;
        let ii = i.borrow();

        let bv = ii.b.v.as_str();
        let cv = ii.c.v.as_str();

        (bv.into(), cv.into())
    }
}

// parent inner
struct I {
    c: C,
    b: B,
}

// child
struct C {
    i: Weak<RefCell<I>>,
    v: String,
}

// child
struct B {
    i: Weak<RefCell<I>>,
    v: String,
}

impl B {
    fn update_cv(&self) {
        if let Some(i) = self.i.upgrade() {
            let mut ii = RefCell::borrow_mut(&i);
            ii.c.v.push_str("b.udpate_cv");
        }
    }
}

fn main() {
    let p = P::new();
    p.update_bv_cv();
    let (bv, cv) = p.get_bv_cv();
    println!("b.v: {bv}\nc.v: {cv}");
}

rust playground

ni65a41a

ni65a41a1#

来自a cross post in URLO
这里有一个安全的解决方案:

let i = {
    // b update c.v
    let ref_i = RefCell::borrow(&self.i); // RefCell::<I>::borrow::<'r>(&'r self) -> Ref<'r, I>
    ref_i.b.i.clone() // throw away 'r via cloning Weak
};
update_cv_from_b(&i);

fn update_cv_from_b(i: &Weak<RefCell<I>>) {
    if let Some(i) = i.upgrade() {
        let mut ii = RefCell::borrow_mut(&i);
        ii.c.v.push_str("b.udpate_cv");
    }
}

playground

相关问题