0
0
Rustprogramming~10 mins

Why traits are used in Rust - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why traits are used
Define Trait
Implement Trait for Types
Use Trait Methods
Write Generic Code Using Traits
Achieve Code Reuse & Polymorphism
Traits define shared behavior. Types implement traits. Code uses traits to work with many types uniformly.
Execution Sample
Rust
trait Speak {
    fn speak(&self);
}

struct Dog;

impl Speak for Dog {
    fn speak(&self) { println!("Woof!"); }
}

fn make_speak(animal: &impl Speak) {
    animal.speak();
}

fn main() {
    let dog = Dog;
    make_speak(&dog);
}
Defines a trait Speak, implements it for Dog, and calls speak via a generic function.
Execution Table
StepActionEvaluationResult
1Define trait Speak with method speak()Trait Speak createdTrait Speak available
2Define struct DogDog type createdDog type available
3Implement Speak for DogDog now has speak()Dog can speak
4Call make_speak with Dog instancemake_speak(&Dog)Dog.speak() called
5Dog.speak() runsPrints "Woof!"Output: Woof!
💡 Function make_speak finishes after calling speak() on Dog
Variable Tracker
VariableStartAfter make_speak callFinal
animalNone&Dog instance&Dog instance
Key Moments - 2 Insights
Why do we use traits instead of just functions?
Traits let us define behavior that many types can share, so functions can accept any type implementing that trait (see execution_table step 4).
How does make_speak know what speak() to call?
Because Dog implements Speak, Rust calls Dog's speak() method when make_speak calls animal.speak() (execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed when make_speak is called with Dog?
A"Meow!"
BNothing
C"Woof!"
DError
💡 Hint
Check execution_table row 5 where Dog.speak() prints "Woof!"
At which step does Dog gain the ability to speak?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at execution_table step 3 where Speak is implemented for Dog
If we add another struct Cat implementing Speak, what changes in make_speak?
Amake_speak can accept Cat without changes
Bmake_speak must be rewritten
Cmake_speak will not work with Cat
DCat must inherit Dog
💡 Hint
Traits allow generic code to accept any type implementing the trait (concept_flow and execution_table step 4)
Concept Snapshot
Traits define shared behavior for types.
Types implement traits to gain that behavior.
Functions can accept any type with a trait.
This enables code reuse and polymorphism.
Traits are like contracts for behavior.
Full Transcript
Traits in Rust are used to define shared behavior that multiple types can implement. This allows writing functions that accept any type implementing a trait, enabling code reuse and flexibility. For example, a trait Speak defines a method speak(). The struct Dog implements Speak by providing its own speak() method. A function make_speak takes any type implementing Speak and calls speak() on it. When make_speak is called with a Dog instance, Dog's speak() runs and prints "Woof!". This shows how traits let us write generic code that works with many types sharing behavior.