-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.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.
Description
This code behaves in a way that is not obvious to me:
fn main() {
fn get_impl_trait() -> impl Default {}
let impl_trait = get_impl_trait();
let mut unit = ();
let modify_unit = || {
let _captured_impl_trait = impl_trait; // (1)
unit = (); // (2)
};
modify_unit();
}
This code compiles without any warnings in stable Rust. But notice, the modify_unit
closure is not declared mut
, but it modifies the unit
variable inside at location (2)
.
However, when you comment out the line marked (1)
, suddenly Rust complains that the closure is not mutable:
Link to the code on the playground.
error[E0596]: cannot borrow `modify_unit` as mutable, as it is not declared as mutable
--> src/main.rs:13:5
|
7 | let modify_unit = || {
| ----------- help: consider changing this to be mutable: `mut modify_unit`
...
13 | modify_unit();
| ^^^^^^^^^^^ cannot borrow as mutable
What is the reason for this behavior?
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.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.