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