expression

Owned async mutex guards

An owned guard keeps an Arc-backed mutex alive without borrowing a caller-owned Arc. It can be moved when its contained type and task requirements permit. Guard lifetime still determines lock occupancy. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 3 Common mistake: Keeping an owned guard indefinitely simply because its lifetime is independent of a borrowed handle.

Formal Syntax & Structure

rust syntaxrust
Arc<Mutex<T>>::lock_owned().await

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use std::sync::Arc;    let lock = Arc::new(tokio::sync::Mutex::new(1));    let mut guard = lock.clone().lock_owned().await;    let task = tokio::spawn(async move { *guard += 2; });    task.await.unwrap();    println!("{}", *lock.lock().await);}
Rust Reference