Start with the idea
A generic bound is monomorphised: one specialised copy per concrete type, direct calls, inlinable, larger binary. dyn Trait keeps one copy and dispatches through a vtable, which is smaller and lets heterogeneous types share a collection at the cost of an indirection.
With a generic T: Trait, callers supply a concrete type and the compiler can specialize code for that type. This often permits inlining and avoids a per-call virtual dispatch, but different concrete instantiations can increase code size and compile time. With dyn Trait, a trait object pairs data with a vtable for methods allowed by object safety. Calls through it use dynamic dispatch, and a collection can hold different concrete implementations behind one pointer type.
Give the interviewer a design example.
Keep learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.