提问人:lost.sof 提问时间:2/27/2023 更新时间:2/27/2023 访问量:643
如何解决错误“已经可变借用:BorrowError”
how to solve the error 'already mutably borrowed: BorrowError'
问:
我对生锈不是很熟练RefCell
我想知道如何解决以下错误。
如果有人能回答我的问题,我将不胜感激。
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}");
}
答:
2赞
ZJPzjp
2/27/2023
#1
这里有一个安全的解决方案:
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");
}
}
评论
0赞
lost.sof
2/28/2023
这不是哎呀,但非常感谢您的回答;)
评论
RefCell
不是围绕 Rust 的借用语义的后门。它只是将规则从编译时移动到运行时,因此您会收到恐慌而不是编译错误。您仍然必须遵循相同的规则:一次一个可变的借用(独占)或几个不可变的借用。