Skip to main content
Module 5 · Lesson 2 of 210/24 in the course~20 min
Module lessons (2/2)

Traits, impl Traits and Clauses where

Lesson preview5 hands-on exercises readyCourse Rust · Module 5 — Generics and Traits

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 trait to define shared behavior contracts between different types.
  • Provide default implementations inside the trait and customize them for specific types.
  • Constrain generics via Trait Bound (T: Summary + Clone, syntax impl Trait and where).

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

  1. 11. Declare and implement a trait
  2. 22. Trait Bound, impl Trait and where
  3. 33. Consistency rule (Orphan Rule)
  4. 44. Compound trait bounds and return impl Trait
  5. 5Practical exercises
  6. 6Check for understanding
Pro Lesson · Module 5 — Generics and Traits

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.