#[derive(Debug)]
struct Node {
prev: Option<Rc<RefCell<Node>>>,
next: Option<Rc<RefCell<Node>>>,
}
impl Node {
fn foo(&mut self, x: Rc<RefCell<Node>>) {
let mut p = Some(x);
while let Some(ref node) = p {
p = node.borrow().prev.as_ref()
.map_or(None, |x| Some(x.clone()));
}
}
}
error[E0506]: cannot assign to p because it is borrowed
impl Node {
fn foo(&mut self, x: Rc<RefCell<Node>>) {
let mut p = Some(x);
while let Some(ref node) = p {
let temp = node.borrow().prev.as_ref()
.map_or(None, |x| Some(x.clone()));
p = temp;
}
}
}
This works, but the 'temp' variable is pointless, and I feel like the compiler might be able to optimize this