expression

Thread-safe trait object bounds

A trait object can carry additional auto-trait bounds. A boxed job sent to a worker needs Send, and its captured data must satisfy that bound. The job is called once by the worker. Expected stdout (not executed): 42 Common mistake: Assuming every dyn FnOnce object is Send without expressing the bound.

Formal Syntax & Structure

rust syntaxrust
Box<dyn FnOnce() -> T + Send + 'static>

Working Example

Example Snippetrust
fn main() {    let job: Box<dyn FnOnce() -> u32 + Send> = Box::new(|| 7 * 6);    let h = std::thread::spawn(job);    println!("{}", h.join().unwrap());}
Rust Reference