5 months ago
27 lines535 B
trait Describe { | |
fn describe(&self) -> String; | |
} | |
| |
struct Dog; | |
struct Robot { model: u32 } | |
| |
impl Describe for Dog { | |
fn describe(&self) -> String { "a good dog".into() } | |
} | |
| |
impl Describe for Robot { | |
fn describe(&self) -> String { format!("robot model {}", self.model) } | |
} | |
| |
fn introduce<T: Describe>(thing: &T) { | |
println!("This is {}", thing.describe()); | |
} | |
| |
fn main() { | |
introduce(&Dog); | |
introduce(&Robot { model: 42 }); | |
} | |
| |
// Try changing the values above and run it again. | |
| |
// Questions? Ask in the course forum. |