I want to return a value from a function which is protected by a Mutex
, but cannot understand how to do it properly. This code does not work:
use std::sync::{Arc, Mutex};
fn func() -> Result<(), String> {
let result_my = Arc::new(Mutex::new(Ok(())));
let result_his = result_my.clone();
let t = std::thread::spawn(move || {
let mut result = result_his.lock().unwrap();
*result = Err("something failed".to_string());
});
t.join().expect("Unable to join thread");
let guard = result_my.lock().unwrap();
*guard
}
fn main() {
println!("func() -> {:?}", func());
}
The compiler complains:
error[E0507]: cannot move out of borrowed content
--> src/main.rs:16:5
|
16 | *guard
| ^^^^^^ cannot move out of borrowed content
The best solution I found so far is to wrap the result into an
Option
and then take it out:It seems better than the
mem::replace
solution proposed by @SBSTP because there is no need to construct an emptyT
for swapping, and it prevents multiple extractions.You can use
mem::replace
to transfer ownership of a mutable reference by replacing it with a new value. (the old value is returned and moved)In Rust 1.15, you can use
Arc::try_unwrap
andMutex::into_inner
: