Why is mutable borrowing not allowed in both the assignment and the else block of an if-let statement in Rust?
struct Parent {
a: Option<()>,
b: (),
}
impl Parent {
fn mutate(&mut self) {}
}
fn main() {
let mut parent = Parent { a: Some(()), b: () };
if let Some(value) = &mut parent.a {
//some mutation on value
} else {
parent.mutate(); //error: cannot borrow `parent` as mutable more than once at a time
}
}
Replacing if-else with the equivalent code below compiles but is not as nice.
if let Some(value) = &mut parent.a {
//some mutation on value
}
if let None = &mut parent.a { //or parent.a.is_none()
parent.mutate();
}