Chapter 25: Traits, Rust’s Core Abstraction
Prerequisites
You will understand
- Traits as named capabilities, not interfaces
- Static dispatch via monomorphization
- When to use
impl Traitvsdyn Trait
Reading time
35 min
+ 20 min exercises
You'll need this for Chapter 42
Advanced traits (trait objects, GATs, supertraits) build directly on the capability model you learn here.
Ch 42: Advanced Traits →
Capability Map
Traits as Named Capabilities
Monomorphization
One Generic Function, Many Concrete Instantiations
Here is dynamic dispatch with nothing hidden: the fat pointer, the vtable, and the indirect jump.
Interactive simulation (requires JavaScript): Box<dyn Speak> is a fat pointer — a data pointer to the heap value plus a vtable pointer into static memory; calling speak() looks up the function in the vtable and jumps, costing one indirection versus the inlined static dispatch of generics.