Created
July 11, 2019 23:18
Star
You must be signed in to star a gist
Code shared from the Rust Playground
This file contains 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
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