Skip to content

Instantly share code, notes, and snippets.

@bvssvni
Created January 26, 2014 00:07
Show Gist options
  • Save bvssvni/8625865 to your computer and use it in GitHub Desktop.
Save bvssvni/8625865 to your computer and use it in GitHub Desktop.
pub struct Player {
name: ~str
}
pub struct Enemy {
name: ~str
}
pub trait Entity {
fn get_name(&self) -> ~str;
}
impl Entity for Player {
fn get_name(&self) -> ~str {
self.name.clone()
}
}
impl Entity for Enemy {
fn get_name(&self) -> ~str {
self.name.clone()
}
}
fn main() {
let x = &Player {name: ~"player1"} as &Any;
if (x.is::<Player>()) {
let y = x.as_ref::<Player>().unwrap();
println!("{}", y.get_name());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment