Closed
Description
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Clone)]
struct X {
x: Rc<RefCell<Option<X>>>,
}
fn main() {
let mut x = X {
x: Rc::new(RefCell::new(None)),
};
loop {
let tmp = match *x.x.borrow() {
Some(ref y) => y.clone(),
_ => break,
};
x = tmp;
} // try: `while let Some(ref y) = *x.x.borrow() { .. }
}
But
error[E0506]: cannot assign to `x` because it is borrowed
--> src/main.rs:22:9
|
20 | while let Some(ref y) = *x.x.borrow() {
| ------------
| | |
| | ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `std::cell::Ref<'_, std::option::Option<X>>`
| borrow of `x` occurs here
| a temporary with access to the borrow is created here ...
21 | let tmp = y.clone();
22 | x = tmp;
| ^ assignment to borrowed `x` occurs here