Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created July 11, 2019 23:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rust-play/ddc37a8260a9d39d97f02d5f76d90c81 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
struct Player {
player_name: String,
real_name: String,
level: u8,
health: u8,
}
struct Monster {
name: String,
subtype: String,
hit_dice: u8,
health: u8,
}
trait Creature {
fn health(&self) -> u8 { self.health };
fn cur_health(&self) -> String {
format!("Health: {}", self.health)
}
}
impl Creature for Player {}
impl Creature for Monster {}
fn main() {
let player_1 = Player {
player_name: "Aragorn".to_string(),
real_name: "Viggo Mortensen".to_string(),
level: 20,
health: 120,
};
let monster_1 = Monster {
name: "Orc".to_string(),
subtype: "Captain".to_string(),
hit_dice: 7,
health: 21,
};
println!(" Player 1: {}", player_1.cur_health());
println!("Monster 1: {}", monster_1.cur_health());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment