Traits and Generics
Traits
Shared Behavior
A trait defines methods a type can implement. Implementing the trait gives the type that behavior.
Program
Play the program to implement a Greet trait for a struct.
traits.rs
Replay: real traced execution (multi-file project)
trait Greet {
fn hello(&self) -> String;
}
struct Person {
name: String,
}
impl Greet for Person {
fn hello(&self) -> String {
format!("Hi, {}", self.name)
}
}
fn main() {
let p = Person { name: String::from("Ada") };
let msg = p.hello();
println!("{msg}");
}
p ← (empty)
15fn main() {16 let → (empty)p = Person { name: String::from("Ada") };17 let msg = p.hello();18 println!("{msg}");msg ← "Hi, Ada"
16 let p = Person { name: String::from("Ada") };17 let ms→ "Hi, Ada"g = p.hello();18 println!("{msg}");19}outputHi, Ada
Follow the Trait Call
pis aPersonwith nameAda.p.hello()calls theGreetbehavior implemented forPerson.- Inside the method,
self.nameisAda. format!("Hi, {}", self.name)returnsHi, Ada.msgstores that text and the program printsHi, Ada. | step | value | | --- | --- | |p|Person { name: "Ada" }| |self.name| Ada | | return value | Hi, Ada | | stdout | Hi, Ada |
trait
`trait Greet` declares required methods.
impl Trait for Type
`impl Greet for Person` provides the behavior.
method call
`p.hello()` runs the implemented method.
Exercise: traits.rs
Reproduce the output Hi, Ada, then trace how p.hello() reads self.name as Ada and returns Hi, Ada.