std-method

Mutex::lock: wait for exclusive access

Acquire exclusive access, waiting if necessary. Handle possible poisoning before using the protected value. This operation is not guaranteed to be fair. Expected stdout (not executed): [1, 2] Common mistake: Assuming the lock cannot block or fail.

Formal Syntax & Structure

rust syntaxrust
m.lock() -> LockResult<MutexGuard<'_ , T>>

Working Example

Example Snippetrust
fn main() {    use std::sync::Mutex;    let m = Mutex::new(vec![1]);    { let mut guard = m.lock().unwrap(); guard.push(2); }    println!("{:?}", m.into_inner().unwrap());}
Rust Reference