Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chapter 25: Traits, Rust’s Core Abstraction

You will understand

  • Traits as named capabilities, not interfaces
  • Static dispatch via monomorphization
  • When to use impl Trait vs dyn Trait

Reading time

35 min
+ 20 min exercises
Capability Map

Traits as Named Capabilities

Vec <i32> Debug Clone IntoIterator Default
Monomorphization

One Generic Function, Many Concrete Instantiations

fn largest<T: PartialOrd>(...) largest_i32 largest_f64 largest_Point static dispatch specialize for the types actually used

Step 1 - The Problem