2013-08-12 22:18:47 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-05-05 20:56:44 -05:00
|
|
|
|
2013-08-12 22:18:47 -05:00
|
|
|
struct Dog {
|
2014-05-12 19:56:43 -05:00
|
|
|
name : StrBuf
|
2013-08-12 22:18:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
trait Barks {
|
2014-05-12 19:56:43 -05:00
|
|
|
fn bark(&self) -> StrBuf;
|
2013-08-12 22:18:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Barks for Dog {
|
2014-05-12 19:56:43 -05:00
|
|
|
fn bark(&self) -> StrBuf {
|
|
|
|
return format!("woof! (I'm {})", self.name).to_strbuf();
|
2013-08-12 22:18:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn main() {
|
2014-05-12 19:56:43 -05:00
|
|
|
let snoopy = box Dog{name: "snoopy".to_strbuf()};
|
|
|
|
let bubbles = box Dog{name: "bubbles".to_strbuf()};
|
2014-05-05 20:56:44 -05:00
|
|
|
let barker = [snoopy as Box<Barks>, bubbles as Box<Barks>];
|
2013-08-12 22:18:47 -05:00
|
|
|
|
|
|
|
for pup in barker.iter() {
|
2013-09-29 21:23:57 -05:00
|
|
|
println!("{}", pup.bark());
|
2013-08-12 22:18:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|