I'm trying to play about with multithreading in Rust and I've come across something I'd consider rather trivial. Here's the code snippet:
use std::thread;
use std::sync::{Arc, Mutex};
fn main() {
let vec: Vec<i32> = vec!(1, 2, 3);
let shared = Arc::new(Mutex::new(vec));
let clone = shared.clone();
let join_handle = thread::spawn(move || {
let mut data = clone.lock().unwrap();
data.push(5);
});
join_handle.join().unwrap();
let clone = shared.clone();
let vec = try!(clone.lock());
println!("{:?}", *vec);
}
So, my issue is on the line let vec = try!(clone.lock())
. This causes the following compiler error:
error: mismatched types [E0308]
return $ crate :: result :: Result :: Err (
^
note: in this expansion of try! (defined in <std macros>)
help: run `rustc --explain E0308` to see a detailed explanation
note: expected type `()`
note: found type `std::result::Result<_, _>`
To me, this doesn't make a lot of sense. clone.lock()
returns TryLockResult<MutexGuard<T>>
, which essentially translates to Result<MutexGuard<T>, PoisonedError<MutexGuard<T>>
, which means try!(clone.lock())
should resolve to either a throw or MutexGuard<T>
.
Am I fundamentally misunderstanding something here?