Most efficient when contention is low, acquiring the lock is a single atomic swap, and releasing it just 1 more atomic swap.
Example
usestd::sync::Arc;usetry_lock::TryLock;// a thing we want to share
structWidget{name: String,}// lock it up!
letwidget1=Arc::new(TryLock::new(Widget{name: "Spanner".into(),}));letwidget2=widget1.clone();// mutate the widget
letmutlocked=widget1.try_lock().expect("example isn't locked yet");locked.name.push_str(" Bundle");// hands off, buddy
letnot_locked=widget2.try_lock();assert!(not_locked.is_none(),"widget1 has the lock");// ok, you can have it
drop(locked);letlocked2=widget2.try_lock().expect("widget1 lock is released");assert_eq!(locked2.name,"Spanner Bundle");