std-method
RwLock::try_read and try_write
Try to acquire either access mode without blocking. A writer cannot acquire exclusive access while a reader guard remains active. Distinguish an unavailable lock from a poison error. Expected stdout (not executed): true true Common mistake: Treating try_write as an atomic upgrade of a held read guard.
Formal Syntax & Structure
rust syntaxrust
lock.try_read(); lock.try_write()Working Example
Example Snippetrust
fn main() { use std::sync::{RwLock, TryLockError}; let l = RwLock::new(5); let g = l.read().unwrap(); println!("{}", matches!(l.try_write(), Err(TryLockError::WouldBlock))); drop(g); println!("{}", l.try_write().is_ok());}