What is the difference between impl Trait and dyn Trait

impl Trait uses static dispatch for known types at compile time, while dyn Trait uses dynamic dispatch for runtime polymorphism via trait objects.

Use impl Trait for static dispatch where the compiler knows the concrete type at compile time, and dyn Trait for dynamic dispatch where the type is resolved at runtime via a pointer. impl Trait is used in function arguments or return types to enforce a specific type without naming it, while dyn Trait creates a trait object that can hold any type implementing the trait.

// Static dispatch: compiler generates specific code for each type
fn process_static(item: impl Display) { }

// Dynamic dispatch: uses a pointer to a trait object
fn process_dynamic(item: &dyn Display) { }