Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.