0
0
Rustprogramming~10 mins

Generics with traits in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Generics with traits
Define trait with methods
Define generic function or struct
Specify trait bounds on generic type
Call function or use struct with type implementing trait
Trait methods available on generic type
Program runs using trait methods on generic type
This flow shows how a trait defines behavior, generics use trait bounds to require that behavior, and then the program runs using those trait methods on generic types.
Execution Sample
Rust
trait Speak {
    fn speak(&self) -> String;
}

fn say_it<T: Speak>(item: T) {
    println!("{}", item.speak());
}

struct Dog;
impl Speak for Dog {
    fn speak(&self) -> String { "Woof!".to_string() }
}

say_it(Dog);
This code defines a trait Speak, a generic function say_it requiring Speak, implements Speak for Dog, and calls say_it with Dog.
Execution Table
StepActionEvaluationResult
1Define trait Speak with method speak()Trait Speak createdTrait Speak ready
2Define generic function say_it<T: Speak>Function say_it requires T to implement Speaksay_it ready
3Define struct DogStruct Dog createdDog ready
4Implement Speak for DogDog now has speak() methodDog implements Speak
5Call say_it(Dog)Check Dog implements SpeakTrue, proceed
6Inside say_it, call item.speak()Dog.speak() called"Woof!" returned
7Print outputOutput printedWoof!
8End of programNo more codeProgram ends
💡 Program ends after printing "Woof!" from Dog's speak method
Variable Tracker
VariableStartAfter Call
itemN/ADog instance passed to say_it
item.speak()N/A"Woof!" string returned
Key Moments - 3 Insights
Why do we write <T: Speak> in the function?
The <T: Speak> means the function only accepts types T that implement the Speak trait, so we can safely call speak() on item. See execution_table step 2 and 5.
What happens if we call say_it with a type that does not implement Speak?
The code will not compile because the trait bound <T: Speak> is not satisfied. This is checked at compile time before step 5.
How does Dog implement the speak() method?
Dog provides its own version of speak() inside impl Speak for Dog, so when say_it calls item.speak(), Dog's method runs. See execution_table step 4 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output printed at step 7?
A"Woof!"
B"Meow!"
C"Hello!"
DNo output
💡 Hint
Check the Result column at step 7 in execution_table
At which step does the program check that Dog implements the Speak trait?
AStep 6
BStep 3
CStep 5
DStep 2
💡 Hint
Look for 'Check Dog implements Speak' in execution_table
If we remove the trait bound <T: Speak> from say_it, what happens?
AProgram runs normally
BCompilation error because speak() may not exist
CDog cannot be created
Dsay_it cannot be called
💡 Hint
Refer to key_moments about trait bounds and compilation checks
Concept Snapshot
trait Speak { fn speak(&self) -> String; }
fn say_it<T: Speak>(item: T) { item.speak(); }
impl Speak for Dog { fn speak(&self) -> String { "Woof!".to_string() } }
Generics use trait bounds to require behavior.
Trait methods can be called on generic types that implement the trait.
This ensures safe, flexible code.
Full Transcript
This example shows how Rust uses generics with traits. First, a trait Speak is defined with a method speak(). Then a generic function say_it is defined, requiring its type T to implement Speak. A struct Dog is created and implements Speak by providing its own speak() method. When say_it is called with Dog, the program checks that Dog implements Speak, then calls Dog's speak() method, printing "Woof!". This ensures that only types with the required behavior can be used with say_it, making the code safe and flexible.