Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Traits & Impls

Traits

Marker Traits

Traits with no methods:

trait Marker

Methods

trait Greet:
  fn hello(self) -> String
  fn goodbye(self) -> String

Default Implementations

Methods can have a default body:

trait WithDefaults:
  fn required(self) -> i64
  fn optional(self) -> String:
    "default"

Associated Types

Types declared inside a trait, defined by each impl:

trait Iterator:
  type Item
  fn next(self) -> Option<Self::Item>

Associated types can have bounds:

trait Collection:
  type Item: Clone + Debug
  fn first(self) -> Self::Item

Associated Constants

trait HasSize:
  const SIZE: i64

Generic Traits

trait Into<T>:
  fn into(self) -> T

Supertraits

A trait can require other traits with with:

trait Sortable with Ord:
  fn sort(self) -> Self

trait Printable with Display + Debug:
  fn print(self) -> String

Where Clauses

trait Convert<T, U> where T: Clone, U: From<T>:
  fn convert(self, value: T) -> U

Generic Methods

Methods can have their own type parameters:

trait Mapper:
  fn map<U>(self, f: fn(Self) -> U) -> U

Static Methods

Methods without self are static (called on the type, not an instance):

trait Factory:
  fn create() -> Self
  fn default() -> Self

Visibility and Attributes

pvt trait InternalTrait:
  fn internal(self) -> i64

#[some_attr]
trait Documented:
  fn describe(self) -> String

Everything Combined

#[some_attr]
pvt trait FullTrait<T, U: Default> with Clone + Debug where T: Display:
  type Output: Clone
  const MAX: i64
  fn required(self, input: T) -> Self::Output
  fn optional(self) -> U:
    U::default()
  fn static_method() -> Self
  fn generic<V>(self, v: V) -> V where V: Into<T>

Impls

Inherent Impls

Methods on a type (no trait):

impl Point:
  fn new(x: i64, y: i64) -> Point:
    // ...

  fn distance(self) -> f64:
    // ...

Trait Impls

Implementing a trait for a type:

impl Display for Point:
  fn display(self) -> String:
    // ...

Generic Impls

impl<T> Container<T>:
  fn unwrap(self) -> T:
    // ...

impl<T: Clone> Clone for Container<T>:
  fn clone(self) -> Container<T>:
    // ...

Blanket Impls

Implement a trait for all types matching a bound:

impl<T> Show for T where T: Display:
  fn show(self) -> String:
    // ...

Multi-Clause Methods

Pattern matching on self in impl methods:

impl<T, E> Result<T, E>:
  fn is_ok(self) -> bool:
    (Ok(_)): true
    (Err(_)): false

The body contains (patterns): body clauses, like a match on the arguments.

Associated Type and Const Impls

impl Iterator for Counter:
  type Item = i64
  fn next(self) -> Option<i64>:
    // ...

impl HasSize for Point:
  const SIZE: i64 = 2

Private Methods and Impls

impl Point:
  fn public_method(self) -> i64:
    // ...
  pvt fn helper(self) -> i64:
    // ...

pvt impl InternalTrait for Point:
  fn internal(self) -> i64:
    // ...

Everything Combined

#[some_attr]
pvt impl<T, U: Default> FullTrait<T, U> for FullBellsAndWhistles<T, U> where T: Display:
  type Output = T
  const MAX: i64 = 100
  fn required(self, input: T) -> T:
    // ...
  fn optional(self) -> U:
    // ...
  fn static_method() -> Self:
    // ...
  fn generic<V>(self, v: V) -> V where V: Into<T>:
    // ...