Version: 1.1.0

Trait declarations

A trait declaration lists required methods and may include default implementations.

Essentials

  • A required method ends with ....
  • A default method has a real body.
  • Traits can require other traits.
  • Associated aliases name types that conformers must pick.
  • A struct must implement every required method, not just the ones it uses.

Example

trait Incrementable:
    fn inc(mut self):
        ...

fn bump[T: Incrementable](mut value: T):
    value.inc()

Keep in mind

Default methods are for convenience. If the default would be wrong for a conformer, make the method required.