2018-08-30 14:18:55 +02:00
|
|
|
//@ run-pass
|
2014-05-05 18:56:44 -07:00
|
|
|
|
2013-08-12 20:18:47 -07:00
|
|
|
struct Dog {
|
2014-05-22 16:57:53 -07:00
|
|
|
name : String
|
2013-08-12 20:18:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trait Barks {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn bark(&self) -> String;
|
2013-08-12 20:18:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Barks for Dog {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn bark(&self) -> String {
|
2014-06-26 08:15:14 +02:00
|
|
|
return format!("woof! (I'm {})", self.name);
|
2013-08-12 20:18:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn main() {
|
2022-07-07 04:36:10 +02:00
|
|
|
let snoopy = Box::new(Dog{name: "snoopy".to_string()});
|
|
|
|
|
let bubbles = Box::new(Dog{name: "bubbles".to_string()});
|
2019-05-28 14:47:21 -04:00
|
|
|
let barker = [snoopy as Box<dyn Barks>, bubbles as Box<dyn Barks>];
|
2013-08-12 20:18:47 -07:00
|
|
|
|
2015-01-31 12:20:46 -05:00
|
|
|
for pup in &barker {
|
2013-09-29 19:23:57 -07:00
|
|
|
println!("{}", pup.bark());
|
2013-08-12 20:18:47 -07:00
|
|
|
}
|
|
|
|
|
}
|