Hello,
By my reasoning, the two while let -loop headers in the program below should be equivalent, but the commented-out one causes a panic, as the borrowed value is not dropped by the time control enters the loop body. Or am I missing something here?
This can be worked around by introducing an explicit inner let block, as shown in the code.
Is there some reason to keep the value alive so long?
After some googling, this seems to be connected to Non-Lexical Lifetimes development. Would this be changed as that work is complete?
use std::cell::RefCell;
fn main() {
let v = RefCell::new(vec![1,2,3]);
//while let Some(i) = v.borrow_mut().pop() { // this panics with BorrowMutError
while let Some(i) = {let mut vv = v.borrow_mut(); vv.pop()} { // inner let is a workaround for the panic
println!("head was {:?}, next is {:?}", i , v.borrow_mut().pop());
}
}
Output:
head was 3, next is Some(2)
head was 1, next is None