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

A trait is a collection of methods defined for an unknown type: Self. They can access other methods declared in the same trait.

Traits can be implemented for any data type. In the example below, we define Animal, a group of methods. The Animal trait is then implemented for the Sheep data type, allowing the use of methods from Animal with a Sheep.

struct Sheep { naked: bool, name: str }

trait Animal {
    // Associated function signature; `Self` refers to the implementor type.
    fn new(name: str) -> Self;

    // Method signatures; these will return a string.
    fn name(self) -> str;
    fn noise(self) -> str;

    // Traits can provide default method definitions.
    fn talk(self) {
        println!("{} says {}", self.name(), self.noise());
    }
}

impl Sheep {
    fn is_naked(self) -> bool {
        self.naked
    }

    fn shear(self) -> Self {
        if self.is_naked() {
            // Implementor methods can use the implementor's trait methods.
            println!("{} is already naked...", self.name());
            self
        } else {
            println!("{} gets a haircut!", self.name);
            Sheep { naked: true, ..self }
        }
    }
}

// Implement the `Animal` trait for `Sheep`.
impl Animal for Sheep {
    // `Self` is the implementor type: `Sheep`.
    fn new(name: str) -> Sheep {
        Sheep { name: name, naked: false }
    }

    fn name(self) -> str {
        self.name
    }

    fn noise(self) -> str {
        if self.is_naked() {
            "baaaaah?"
        } else {
            "baaaaah!"
        }
    }

    // Default trait methods can be overridden.
    fn talk(self) {
        // For example, we can add some quiet contemplation.
        println!("{} pauses briefly... {}", self.name, self.noise());
    }
}

pub fn main() {
    // Type annotation is necessary in this case.
    let dolly: Sheep = Animal::new("Dolly");
    // TODO ^ Try removing the type annotations.

    dolly.talk();
    dolly.shear();
    dolly.talk();
}