std-type

MutexGuard: scoped access

The guard represents temporary permission to access the protected value. Releasing it normally unlocks the mutex. A short lexical scope makes the unlock point clear. Expected stdout (not executed): 4 Common mistake: Acquiring the same lock again while its guard is still held.

Formal Syntax & Structure

rust syntaxrust
MutexGuard<'a, T>; drop(guard)

Working Example

Example Snippetrust
fn main() {    use std::sync::Mutex;    let m = Mutex::new(3);    let mut g = m.lock().unwrap();    *g += 1;    drop(g);    println!("{}", *m.lock().unwrap());}
Rust Reference