Traits for Rusts
Traits in Rust define shared behaviour that different types can implement, and generics let code work over many types with trait bounds. Together they provide static polymorphism with no runtime cost. Closures and iterators build on traits like Fn and Iterator to give expressive, lazy data processing.
Stage: Abstraction · Part of the Rust roadmap · Last reviewed 19 November 2026
What to learn in Traits
- Define a trait and implement it for a struct
- Write a generic function with a trait bound
- Use trait objects with dyn when types vary at runtime
- Chain iterator adapters such as map, filter and collect
- Explain the difference between Fn, FnMut and FnOnce
Traits interview questions
What is the difference between generics and trait objects?
Generics are resolved at compile time through monomorphisation, giving fast code but one copy per type. Trait objects, written dyn Trait, use dynamic dispatch through a vtable, allowing mixed types in one collection at a small runtime cost.
Are Rust iterators lazy?
Yes. Adapters like map and filter do nothing until a consumer, such as collect, sum or a for loop, drives them. This lets chains run in one pass without building intermediate collections, and the optimiser often turns them into code as fast as a manual loop.
Where Traits fits in the Rust roadmap
Traits belongs to the Abstraction stage (advanced level), which takes about 28 hours. Traits and errors.