Skip to content
uencoder

Trait

A trait defines a set of methods for shared behavior; types implement the trait to provide those methods — the interface and abstraction mechanism of Rust/Scala.

Purpose

Define shared-behavior interfaces, supporting composition over inheritance.

Mechanism

A trait declares method signatures (possibly with default implementations). Types impl Trait to provide method bodies. Trait bounds constrain generic types. Trait objects (dyn Trait) enable dynamic dispatch. Supertraits provide inheritance.

Use cases

  • Rust programming
  • Interface abstraction
  • Generic constraints

References