-
-
Save ZhangHanDong/8d4e1b42ece3853da4ff2a9a13a5cfdf to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or 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
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