Module lessons (2/2)
Traits, impl Traits and Clauses where
Define shared behaviors with trait, default implementations, trait bound T: Summary + Clone, where, and blanket impl.
What you'll learn in this lesson
- Declare a
traitto define shared behavior contracts between different types. - Provide default implementations inside the
traitand customize them for specific types. - Constrain generics via Trait Bound (
T: Summary + Clone, syntaximpl Traitandwhere).
Overview
Rust has no classes or inheritance between structs. Instead, use traits — similar to Go's interfaces or Haskell's type classes — with explicit implementation (impl Trait for Tipo).
A trait lists the required method signatures and can optionally provide a default body that calls other methods of the same trait:
Sections covered in this lesson
- 11. Declare and implement a
trait - 22. Trait Bound,
impl Traitandwhere - 33. Consistency rule (Orphan Rule)
- 44. Compound trait bounds and return
impl Trait - 5Practical exercises
- 6Check for understanding
Unlock "Traits, impl Traits and Clauses where" and the full Course Rust curriculum
Define shared behaviors with `trait`, default implementations, trait bound `T: Summary + Clone`, `where`, and blanket impl.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course Rust plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.