Closed
Description
Hi. I was trying the new Mutex::new()
in const
a context (new in rust 1.63) and I'm able to mutate it's value:
I tried this code:
use std::sync::Mutex;
const VAR: Mutex<usize> = Mutex::new(0);
fn main() {
println!("var: {}", *VAR.lock().unwrap());
*VAR.lock().unwrap() = 3;
println!("var: {}", *VAR.lock().unwrap());
}
The output is
var: 0
var: 0
Playground link here.
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
andjo403 commentedon Aug 11, 2022
The problem is that you try to mutate a const this creates a temporary that you mutate, if you change const to static you get the expected result.
cc #40543
orium commentedon Aug 11, 2022
Closing since this is the expected behavior: https://www.reddit.com/r/rust/comments/wltcf8/comment/ijvx9ym/?context=3
riking commentedon Aug 17, 2022
Diagnostics part of issue is covered in #40543