If you borrow immutably in an if test, the borrow lasts for the whole if expression. This means that mutable borrows in the clauses will cause the borrow checker to fail.
This can also happen when borrowing in the match expression, and needing a mutable borrow in one of the arms.
See here for an example where the if borrows boxes, which causes the nearest upwards @mut to freeze. Then remove_child() which needs to borrow mutably conflicts.
https://github.com/mozilla/servo/blob/master/src/servo/layout/box_builder.rs#L387-L411
Updated example from @Wyverald
fn main() {
let mut vec = vec!();
match vec.first() {
None => vec.push(5),
Some(v) => unreachable!(),
}
}
If you borrow immutably in an
iftest, the borrow lasts for the wholeifexpression. This means that mutable borrows in the clauses will cause the borrow checker to fail.This can also happen when borrowing in the match expression, and needing a mutable borrow in one of the arms.
See here for an example where the if borrows boxes, which causes the nearest upwards
@mutto freeze. Thenremove_child()which needs to borrow mutably conflicts.https://github.com/mozilla/servo/blob/master/src/servo/layout/box_builder.rs#L387-L411
Updated example from @Wyverald