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