Skip to content

Instantly share code, notes, and snippets.

@ZhangHanDong
Forked from rust-play/playground.rs
Last active July 19, 2020 10:22
Show Gist options
  • Save ZhangHanDong/8d4e1b42ece3853da4ff2a9a13a5cfdf to your computer and use it in GitHub Desktop.
Save ZhangHanDong/8d4e1b42ece3853da4ff2a9a13a5cfdf to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
pub struct Monster {
hp: u8, // health points
sp: u8, // spell points
friends: Vec<Friend>,
}
pub struct Friend {
loyalty: u8,
}
impl Monster {
pub fn final_breath(&mut self) {
// Error
// if let Some(friend) = self.friends.first() {
// self.heal(friend.loyalty);
// println!("Healing for {}", friend.loyalty);
// }
self.change_heal();
self.display_heal();
}
fn change_heal(&mut self){
if let Some(friend) = self.friends.first() {
self.heal(friend.loyalty);
}
}
fn display_heal(&self){
if let Some(friend) = self.friends.first() {
println!("Healing for {}", friend.loyalty);
}
}
fn heal(&mut self, amount: u8) {
self.hp += amount;
self.sp -= amount;
}
}
fn main() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment