trait-concept

Rust Auto-trait bounds on trait objects

A trait-object type can include auto-trait requirements such as Send, in addition to its principal trait. This records a capability needed by an API. It does not itself start a thread or make a non-Send implementation safe. Expected output (not executed): 6 Why: Count meets the Job and Send requirements; the function consumes the box. Common mistake: Assuming Send implies Sync or that adding Send changes a type that does not implement it.

Formal Syntax & Structure

rust syntaxrust
Box<dyn Trait + Send>

Working Example

Example Snippetrust
trait Job { fn run(&self) -> u32; }struct Count(u32);impl Job for Count { fn run(&self) -> u32 { self.0 } }fn accepted(job: Box<dyn Job + Send>) -> u32 { job.run() }fn main() { println!("{}", accepted(Box::new(Count(6)))); }
Rust Reference