← Rust Programming learning pathRust Programming · Module 8 of 10
Traits and generics
Share behaviour with traits and write generic code.
📖 Key ideas
- A trait defines shared behaviour: trait Summary { fn summarize(&self) -> String; }.
- impl Summary for Article { ... }.
- Generics: fn largest<T: PartialOrd>(list: &[T]) -> &T.
- Trait bounds restrict generic types.
- Common traits: Debug, Clone, Display, Iterator.
💡 Real-life example: Traits are skill badges: any type that earns the badge can perform that skill.
TraitShared behaviour definition
Trait boundConstraint on a generic type
GenericType parameter <T>
DisplayTrait for user-facing printing