-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
let and if let have different closure capture behaviors #153982
Copy link
Copy link
Open
Labels
A-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-patternsRelating to patterns and pattern matchingRelating to patterns and pattern matchingC-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamRelevant to the language team
Metadata
Metadata
Assignees
Labels
A-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-patternsRelating to patterns and pattern matchingRelating to patterns and pattern matchingC-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamRelevant to the language team
Type
Fields
Give feedbackNo fields configured for issues without a type.
I tried this code:
I expected the three printed numbers to be the same. Instead, in editions 2021 and 2024, the code prints
48then24then24.It seems that
if letcauses the entire value ofxto be captured, butletandmatchonly capture the relevant field. This seems inconsistent.cc @Nadrieril @meithecatte
Meta
Reproducible on the playground with version
1.96.0-nightly (2026-03-12 3102493c71626b5912d1)and also on version1.94.0.However, testing on Godbolt with version
1.93.0(which is before #138961), it prints48then48then24.