-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.fixed-by-NLLBugs fixed, but only when NLL is enabled.Bugs fixed, but only when NLL is enabled.
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);
}
Metadata
Metadata
Assignees
Labels
A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.fixed-by-NLLBugs fixed, but only when NLL is enabled.Bugs fixed, but only when NLL is enabled.