I know when borrowing a value from a RefCell
, I can manually drop it to end this borrow.
However, if I use the reference to a RefMut
instead of directly using the RefMut
, the drop trait seems invalid to end this borrow.
So, what happend when trying to drop the &RefMut
? and why the RefMut
will not be dropped during this operation. If the referenced RefMut
is not dropped, when will it be dropped ?
use std::cell::RefCell;
struct Data {
pub x: usize,
}
fn main() {
let c = RefCell::new(Data { x: 42 });
let b = &c.borrow_mut();
drop(b);
let mut d = c.borrow_mut();
d.x = 43;
println!("Hello, world!");
}
output
thread 'main' panicked at 'already borrowed: BorrowMutError', src/main.rs:11:19
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace