Created
June 18, 2013 02:04
-
-
Save benw/5802109 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Fruit { | |
fn eat(&self); | |
} | |
struct Apple { | |
x: int | |
} | |
impl Fruit for Apple { | |
fn eat(&self) { | |
println(fmt!("Crunching on apple (%d)", self.x)); | |
} | |
} | |
struct Banana { | |
y: int | |
} | |
impl Fruit for Banana { | |
fn eat(&self) { | |
println(fmt!("Munching on banana (%d)", self.y)); | |
} | |
} | |
fn main() { | |
let managed: ~[ @Fruit ] = ~[ | |
@Apple{ x: 1 } as @Fruit, | |
@Banana{ y: 2 } as @Fruit | |
]; | |
let owned: ~[ ~Fruit ] = ~[ | |
~Apple{ x: 1 } as ~Fruit, | |
~Banana{ y: 2 } as ~Fruit | |
]; | |
println(~"This eats both fruit, as expected:"); | |
for managed.each |fruit| { | |
fruit.eat(); | |
} | |
println(~"This only eats the apple - why?!"); | |
for owned.each |fruit| { | |
fruit.eat(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment