Skip to content

Cannot mutate the value inside a Mutex created in a const context #100417

@orium

Description

@orium

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

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions