LogoMist

Traits & Impls

Defining traits, trait bounds, and implementation blocks.

Traits

pub trait Drawable
{
    void draw(&self);
}

pub trait Comparable<T> : Eq
{
    i32 cmp(&self, T other);
}

Trait requirements are specified after ::

trait MyTrait : SuperTrait + OtherTrait
{
    void required_method(&self);
    void another(&self);
}

Impl Blocks

impl MyType
{
    void method(&self) { }
}

impl Trait for MyType
{
    void method(&self) { }
}

impl<T> GenericTrait<T> for MyType
{
    void method(&self, T value) { }
}

On this page