mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 15:23:46 +00:00
27 lines
490 B
Rust
27 lines
490 B
Rust
//@ run-pass
|
|
|
|
struct Dog {
|
|
name : String
|
|
}
|
|
|
|
trait Barks {
|
|
fn bark(&self) -> String;
|
|
}
|
|
|
|
impl Barks for Dog {
|
|
fn bark(&self) -> String {
|
|
return format!("woof! (I'm {})", self.name);
|
|
}
|
|
}
|
|
|
|
|
|
pub fn main() {
|
|
let snoopy = Box::new(Dog{name: "snoopy".to_string()});
|
|
let bubbles = Box::new(Dog{name: "bubbles".to_string()});
|
|
let barker = [snoopy as Box<dyn Barks>, bubbles as Box<dyn Barks>];
|
|
|
|
for pup in &barker {
|
|
println!("{}", pup.bark());
|
|
}
|
|
}
|