我对铁 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}");
}
1条答案
按热度按时间ni65a41a1#
来自a cross post in URLO
这里有一个安全的解决方案:
playground