Lock Dropping Behavior Question
⚓ Rust 📅 2025-11-25 👤 surdeus 👁️ 8use tokio::sync::Mutex;
use std::sync::Arc;
#[tokio::main]
async fn main() {
let a = Arc::new(Mutex::new(String::from("Hello")));
foo(a.clone(), a.lock().await.clone()).await;
}
async fn foo(l: Arc<Mutex<String>>, s: String) {
let guard = l.lock().await;
println!("{guard} {s}")
}
Errors:
Compiling playground v0.0.1 (/playground)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.75s
Running `target/debug/playground`
Hello!
When I run this code, it seems to deadlock and no output is emitted. It seems that the lock should be dropped once the inner String is cloned. What am I missing here?
Thanks!
2 posts - 2 participants
🏷️ Rust_feed