Error handling in threads
The Rust Standard Library contains the std::thread::Result
type, which is a specialized Result
type for threads. An example of how to use this is shown in the following code:
use std::fs; use std::thread; fn copy_file() -> thread::Result<()> { thread::spawn(|| { fs::copy("a.txt", "b.txt").expect("Error occurred"); }) .join() } fn main() { match copy_file() { Ok(_) => println!("Ok. copied"), Err(_) => println!("Error in copying file"), } }
We have a function, copy_file()
, that copies a source file to a destination file. This function returns a thread::Result...