mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-11 03:47:30 +02:00
38 lines
629 B
Markdown
38 lines
629 B
Markdown
|
# Traits
|
||
|
|
||
|
Rust lets you abstract over types with traits. They're similar to interfaces:
|
||
|
|
||
|
```rust,editable
|
||
|
trait Greet {
|
||
|
fn say_hello(&self);
|
||
|
}
|
||
|
|
||
|
struct Dog {
|
||
|
name: String,
|
||
|
}
|
||
|
|
||
|
struct Cat; // No name, cats won't respond to it anyway.
|
||
|
|
||
|
impl Greet for Dog {
|
||
|
fn say_hello(&self) {
|
||
|
println!("Wuf, my name is {}!", self.name);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Greet for Cat {
|
||
|
fn say_hello(&self) {
|
||
|
println!("Miau!");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
let pets: Vec<Box<dyn Greet>> = vec![
|
||
|
Box::new(Dog { name: String::from("Fido") }),
|
||
|
Box::new(Cat),
|
||
|
];
|
||
|
for pet in pets {
|
||
|
pet.say_hello();
|
||
|
}
|
||
|
}
|
||
|
```
|