Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- let mut monster: Mammal = MammalT::new();
- println!("monster's HP is {} and Hunger is {}", monster.hp, monster.hunger);
- monster.hunger = 15;
- monster.chkhunger();
- println!("monster's HP is {} and Hunger is {}", monster.hp, monster.hunger);
- }
- struct Mammal {
- hp: int,
- hunger: int
- }
- trait MammalT {
- fn new() -> Self;
- fn chkhunger(&mut self);
- }
- impl MammalT for Mammal {
- fn new() -> Mammal {
- Mammal { hp: 10, hunger: 0 }
- }
- fn chkhunger(&mut self) {
- if self.hunger > 10 {
- self.hp -= 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement