Rust Systems & Tokio (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
Static vs Dynamic Dispatch: `impl Trait` vs `dyn Trait`.
senior LevelrustAdvanced Internals
In Simple Words:
- Static Dispatch (
impl Trait/ Generics): The compiler generates a concrete copy of the function for every specific type (Monomorphization). Zero runtime cost, allows function inlining, but increases binary size. - Dynamic Dispatch (
&dyn Trait/Box<dyn Trait>): Method lookups happen at runtime via a Virtual Method Table (vtable). Allows heterogeneous collections (e.g.Vec<Box<dyn Widget>>), but introduces vtable pointer dereferencing overhead.