Back to library

🦀Rust Traits and Generics

Stop reading Rust traits as 'interfaces with extra steps' and start using them as composable behavior contracts — until you can design a trait-based plugin system with blanket implementations that generates whole APIs from a single impl block.

Applied14 drops~2-week path · 5–8 min/daytechnology

Phase 1Traits as Behavior Contracts

See traits as contracts, not interfaces.

4 drops
  1. A trait is a contract about behavior, not a type

    6 min

    A trait is a contract about behavior, not a type

  2. Trait methods dispatch on the receiver type at compile time

    6 min

    Trait methods dispatch on the receiver type at compile time

  3. A trait bound says 'this generic must be able to do these things'

    7 min

    A trait bound says 'this generic must be able to do these things'

  4. You can implement a trait for a type only if you own one of them

    7 min

    You can implement a trait for a type only if you own one of them

Phase 2Implementing the Standard Library Traits

Implement Display, From, and Iterator on real types.

5 drops
  1. Implement Display once and every formatter learns to print your type

    7 min

    Implement Display once and every formatter learns to print your type

  2. Implement From and you get Into for free

    7 min

    Implement From and you get Into for free

  3. Implement Iterator and inherit hundreds of combinators for free

    8 min

    Implement Iterator and inherit hundreds of combinators for free

  4. Use derive when the impl is mechanical, write it when it isn't

    6 min

    Use derive when the impl is mechanical, write it when it isn't

  5. A trait can ship behavior, not just signatures

    7 min

    A trait can ship behavior, not just signatures

Phase 3Static, Dynamic, and Associated

Choose between impl Trait, dyn Trait, and generic bounds.

4 drops
  1. You're writing a plugin host — three signatures, three different bills

    7 min

    You're writing a plugin host — three signatures, three different bills

  2. Your Iterator-as-trait-object refuses to compile — find the violation

    7 min

    Your Iterator-as-trait-object refuses to compile — find the violation

  3. Your trait has too many generic parameters — try associated types

    8 min

    Your trait has too many generic parameters — try associated types

  4. Your trait needs Display but you didn't say so — the compiler tells you

    7 min

    Your trait needs Display but you didn't say so — the compiler tells you

Phase 4Build a Trait-Based Plugin System

Design a plugin system with blanket implementations.

1 drop
  1. Build a plugin registry where one blanket impl powers every consumer

    8 min

    Build a plugin registry where one blanket impl powers every consumer

Frequently asked questions

What's the difference between a Rust trait and a Java interface?
This is covered in the “Rust Traits and Generics” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
When should I use impl Trait versus dyn Trait?
This is covered in the “Rust Traits and Generics” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
What is a blanket implementation in Rust?
This is covered in the “Rust Traits and Generics” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
How do associated types differ from generic parameters?
This is covered in the “Rust Traits and Generics” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
Why does the orphan rule exist for trait implementations?
This is covered in the “Rust Traits and Generics” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.