std-method
Mutex::try_lock: nonblocking attempt
Attempt acquisition without waiting for another holder to release the lock. Distinguish temporary contention from poisoning. Do not retry in a tight unbounded loop. Expected stdout (not executed): true true Common mistake: Treating a failed nonblocking attempt as proof that the data is invalid.
Formal Syntax & Structure
rust syntaxrust
m.try_lock() -> TryLockResult<MutexGuard<'_, T>>Working Example
Example Snippetrust
fn main() { use std::sync::{Mutex, TryLockError}; let m = Mutex::new(7); let g = m.lock().unwrap(); println!("{}", matches!(m.try_lock(), Err(TryLockError::WouldBlock))); drop(g); println!("{}", m.try_lock().is_ok());}