Traits and You: A Deep Dive
summary
Covers concrete, testable claims: traits separate behavior from data, trait methods can have defaults (Constitution bonus = 2 for dwarf, 1 for half‑orc, 0 default for elf/human), trait bounds make functions accept only types that implement a trait and produce a compile‑time error when trait not implemented, and trait objects let heterogeneous collections work via pointers to heap data. Demonstrates building a spellbook as a Vec<Box<dyn Cast>> with a run method that iterates and calls cast on each entry.
AI-written summary · about
chapters (14)
- 0:00Intro
- 0:19Traits are awesome!
- 0:46I have learned to harness the power of traits....
- 2:07Traits 101: Intro to Traits
- 5:41The constitution bonus for a dwarf is 2
- 6:56The constitution bonus for a half-orc is 1
- 9:41Traits 201: Trait Bounds
- 10:46Let's make a function for speaking Elvish
- 12:51Trait bounds allow a function to only accept types that implement a certain trait
- 13:11Traits 301: Trait Objects
- 14:09Trait objects behave more like traditional objects
- 15:01You cannot add data to a trait object
- 20:41Trait objects are great for heterogenus collections
- 21:13Traits are hard... at first
related talks
27mRe-inventing trait objects
01h 00mTemplate Metaprogramming: Type Traits (part 1 of 2)
59mTemplate Metaprogramming: Type Traits (part 2 of 2)
36mRust's trait system is a proof engine, let's make it prove us an ABI!
33mReplacing #[cfg] with Traits: The Inlinable Dyn Extension Trait Pattern
25mType System Tips for the Real World
15mSupercharging Your Code With Five Little-Known Attributes
01h 01mThe promise of static reflection in C++26: Type Traits without compiler intrinsics
