fn get(x: &mut Option<u128>) -> &u128 {
if let Some(xx) = &*x {
return xx
}
x.insert(12);
&12
}
i'm getting this error:
error[E0502]: cannot borrow `*x` as mutable because it is also borrowed as immutable
--> src/lib.rs:5:5
|
1 | fn get(x: &mut Option<u128>) -> &u128 {
| - let's call the lifetime of this reference `'1`
2 | if let Some(xx) = &*x {
| --- immutable borrow occurs here
3 | return xx
| -- returning this value requires that `*x` is borrowed for `'1`
4 | }
5 | x.insert(12);
| ^^^^^^^^^^^^ mutable borrow occurs here
shouldn't the reborrow die after the if statement?
If the condition can be expressed entirely as pattern matching, then using a ref pattern instead of & lets you defer the reborrow until after the pattern has been determined to successfully match:
fn get(x: &mut Option<u128>) -> &u128 {
if let Some(ref xx) = x {
xx
} else {
x.insert(12)
}
}
(Other changes just to tidy up the code, not because they are needed)