rust - `if` condition remains borrowed in body -
this question has answer here:
i came across behaviour in rust (1.12) cannot explain. have struct implements lazy loading refcell<option<i32>>
, function access data:
struct foo { data: refcell<option<i32>> } impl foo { fn get_data(&self) -> i32 { if self.data.borrow().is_none() { // <--- (a) let d = 1337; self.data.borrow_mut() = some(d); // <--- (b) d } else { self.data.borrow().unwrap() } } }
this compiles yields runtime error: refcell
complains borrow active when attempting borrow_mut
on line (b). issue not occur, however, if change if statement following:
let is_none = self.data.borrow().is_none(); if is_none {
question: why borrow in if condition on line (a) still active inside body of if statement? shouldn't call is_none()
cause borrow end, since i'm holding on bool
afterwards, , not borrowed value?
this due rust's lexical borrowing scope.
as have noticed, borrowing in condition borrows branch. discussed in this issue, , has since moved here.
this limitation fixed in rust 1.13 beta , released in next cycle.
Comments
Post a Comment