std-function

thread::spawn: run a closure on another thread

Spawn a native thread and retain its join handle. The closure and result must meet the transfer and lifetime requirements. Starting the worker can panic if the OS cannot create it. Expected stdout (not executed): 12 Common mistake: Expecting the returned handle to contain the completed result immediately.

Formal Syntax & Structure

rust syntaxrust
thread::spawn(F) -> JoinHandle<T> where F: FnOnce() -> T + Send + 'static, T: Send + 'static

Working Example

Example Snippetrust
fn main() {    let h = std::thread::spawn(|| [2, 4, 6].iter().sum::<i32>());    println!("{}", h.join().unwrap());}
Rust Reference