Trait Objects and dyn
Dynamic dispatch.
The Problem: Mixed Types
Generics with trait bounds work great when each call uses one concrete type. But what if you want a collection of different types that all share a trait? Generics cannot hold a mix.
Trait objects solve this.
What Is a Trait Object?
A trait object is a value referred to through a pointer plus the keyword dyn, like &dyn Trait or Box<dyn Trait>.
It lets you treat many concrete types uniformly at runtime.
All lessons in this course
- Defining Traits
- Trait Objects and dyn
- Static vs Dynamic Dispatch
- Default Methods