Rust theorytheory 0/50 · 0%
Abstraction · medium

23. Trait Objects and dyn

Static versus dynamic dispatch.

`impl Trait` and generics use static dispatch resolved at compile time. `dyn Trait` behind a reference or Box uses a vtable for dynamic dispatch, enabling heterogeneous collections at a small runtime cost. Only object-safe traits can be used as trait objects.

Check your understanding

  1. 1. `Box<dyn Trait>` uses…

  2. 2. Trait objects require the trait to be…