Answers for "rust create mutex"

1

rust create mutex

fn main() {
    let foo = Mutex::new(0);
    // It's often best to just unwrap and panic if the lock is poisoned
    if let Ok(mut lock) = foo.lock() {
        *lock = 2;
        // The mutex is unlocked automatically when lock goes out of scope here
    }
    println!("{:?}", foo); // Mutex { data: 2 }
}
Posted by: Guest on February-04-2022

Browse Popular Code Answers by Language