Skip to main content Link Search Menu Expand Document (external link)

Traits

In Rust, traits are a way to define shared behavior or functionality that types can implement. They provide a mechanism for defining a set of methods or associated constants that can be used across different types, allowing for code reuse and abstraction.

Here are some key points about traits in Rust:

  1. Trait Definition: Traits are defined using the trait keyword followed by the trait name and a set of method signatures or associated constants. For example:
trait MyTrait {
    fn method1(&self);
    fn method2(&mut self);
    const CONSTANT: u32;
}
  1. Implementing Traits: Types can implement traits by providing implementations for each of the trait’s methods and associated constants. This is done using the impl keyword. For example:
struct MyStruct;

impl MyTrait for MyStruct {
    fn method1(&self) {
        // Implementation for method1
    }

    fn method2(&mut self) {
        // Implementation for method2
    }

    const CONSTANT: u32 = 42;
}
  1. Default Implementations: Traits can provide default implementations for some or all of their methods. Types implementing the trait can choose to override these methods or use the default implementation. Default implementations are defined using the default keyword.
trait MyTrait {
    fn method1(&self) {
        // Default implementation for method1
    }

    fn method2(&mut self);
}
  1. Trait Bounds: Trait bounds are used to specify that a generic type parameter must implement a certain trait. This ensures that the generic type can use the methods and functionality defined by the trait. Trait bounds are specified using the where clause or directly in the generic type declaration.
fn my_function<T: MyTrait>(value: T) {
    // Function implementation
}

fn my_function<T>(value: T)
where
    T: MyTrait,
{
    // Function implementation
}
  1. Associated Types: Traits can define associated types, which allow the trait to define types that are associated with the trait but not necessarily specific to each implementation. Associated types are specified using the type keyword.
trait Iterator {
    type Item;

    fn next(&mut self) -> Option<Self::Item>;
}

Traits are a fundamental part of Rust’s type system and are used extensively to enable code reuse, abstraction, and generic programming. They provide a powerful mechanism for defining shared behavior across different types and promoting code flexibility and modularity.