1# TryLock 2 3- [Crates.io](https://crates.io/crates/try-lock) 4- [Docs](https://docs.rs/try-lock) 5 6A light-weight lock guarded by an atomic boolean. 7 8Most efficient when contention is low, acquiring the lock is a single atomic swap, and releasing it just 1 more atomic swap. 9 10## Example 11 12```rust 13use std::sync::Arc; 14use try_lock::TryLock; 15 16// a thing we want to share 17struct Widget { 18 name: String, 19} 20 21// lock it up! 22let widget1 = Arc::new(TryLock::new(Widget { 23 name: "Spanner".into(), 24})); 25 26let widget2 = widget1.clone(); 27 28 29// mutate the widget 30let mut locked = widget1.try_lock().expect("example isn't locked yet"); 31locked.name.push_str(" Bundle"); 32 33// hands off, buddy 34let not_locked = widget2.try_lock(); 35assert!(not_locked.is_none(), "widget1 has the lock"); 36 37// ok, you can have it 38drop(locked); 39 40let locked2 = widget2.try_lock().expect("widget1 lock is released"); 41 42assert_eq!(locked2.name, "Spanner Bundle"); 43``` 44 45