Closed
Description
Borrows in patterns are kept during the whole if let
expression, including the else
branch, which is counterintuitive and annoying.
For example, this doesn't compile.
let mut x = Some(1);
if let Some(&ref p) = x.as_ref() {
// do something with p
} else {
x = Some(2); // error: x is still borrowed
}
To pass the borrow check, you have to write ugly code like this.
let mut x = Some(1);
let mut flag = false;
if let Some(&ref p) = x.as_ref() {
// do something with p
} else {
flag = true;
}
if flag {
x = Some(2);
}