Static versus dynamic dispatch
Quick answer
Dispatch means choosing which implementation of a method to call. Generic trait bounds typically use static dispatch, while a trait object supports selecting behavior through a runtime interface.
Trade-offs at the boundary
A generic function fn f<T: Trait>(x: &T) is instantiated for concrete types and can enable inlining. A &dyn Trait reference carries metadata for calling a compatible implementation indirectly. Dynamic dispatch can reduce duplication in some cases; measure costs rather than assuming either is always faster.
trait Size { fn size(&self) -> u32; }struct Parcel(u32);impl Size for Parcel { fn size(&self) -> u32 { self.0 } }fn report<T: Size>(x: &T) { println!("{}", x.size()); }fn main() { report(&Parcel(5)); }Expected stdout (not executed):
5The caller provides a known concrete type.
trait Size { fn size(&self) -> u32; }struct Bag(u32);impl Size for Bag { fn size(&self) -> u32 { self.0 } }fn report(x: &dyn Size) { println!("{}", x.size()); }fn main() { report(&Bag(8)); }Expected stdout (not executed):
8The interface erases the concrete type at the function boundary; it needs no heap allocation here.
Common mistake
Dynamic dispatch does not inherently require Box or a heap allocation.
Try it yourself
Implement Size for a second type and call each version of report with both types.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Static versus dynamic dispatch — practice quiz
Choose one answer per question, then review the explanation.
What is a possible benefit of static dispatch for a generic function?
A function takes `&dyn Named`. How may it invoke name?
Is dynamic dispatch always slower in a complete application?