Traits
What are Traits?
Section titled “What are Traits?”Traits define behaviors that can be implemented by custom types. They are similar to interfaces in other languages but with some key differences. The Rust definition applies well to Ard:
A trait defines the functionality a particular type has and can share with other types. We can use traits to define shared behavior in an abstract way.
Defining Traits
Section titled “Defining Traits”Traits consist of method signatures that an implementing types must provide:
Here is a trait that is part of the standard library in the scope of the Str
type.
trait ToString { fn to_str() Str}
💡
Str::ToString
is the trait that standardio::print
function accepts and the built-in primitives implement it.
A trait can have multiple methods:
trait Drawable { fn draw() fn get_bounds() Rectangle fn is_visible() Bool}
Implementing Traits
Section titled “Implementing Traits”Use impl TraitName for TypeName
to implement a trait for a specific type:
struct Person { name: Str, age: Int,}
impl Str::ToString for Person { fn to_str() Str { "{@name} is {@age} years old" }}
Using Traits
Section titled “Using Traits”As Function Parameters
Section titled “As Function Parameters”Traits can be used as function parameter types to accept any type that implements the trait:
fn debug(thing: Str::ToString) { io::print(thing.to_str()) thing.name // Error}
let person = Person { name: "Alice", age: 30 }debug(person)