Associated types vs generic type parameters in traits
WHAT IT TESTS: trait design and type-level reasoning. OUTLINE: associated types fix one type per implementer; generics allow many impls; Iterator::Item is the canonical example. RED FLAG: claiming they are interchangeable or that generics are always better.
WHAT IT TESTS: deep understanding of Rust's trait system and when each tool fits. ANSWER OUTLINE: an associated type binds a single output type per impl, so a type implements the trait once and inference stays clean; a generic trait parameter lets a type implement the trait many times with different parameters. Iterator uses Item as an associated type so .next() infers cleanly. RED FLAG: saying they are interchangeable, or that you can implement Iterator multiple times for one type.
Read the original → interview
- #rust
- #traits
- #associated-types
- #generics
- #type-system
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.