Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- let mut monster: MammalType = Mob::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);
- monster.sodoku();
- println!("monster committed sodoku and HP is now {}", monster.hp);
- }
- struct MammalType {
- hp: int,
- hunger: int
- }
- trait Mammal {
- fn new() -> Self;
- fn chkhunger(&mut self);
- }
- impl Mammal for MammalType {
- fn new() -> MammalType {
- MammalType { hp: 10, hunger: 0 }
- }
- fn chkhunger(&mut self) {
- if self.hunger > 10 {
- self.hp -= 1;
- }
- }
- }
- trait Mob : Mammal {
- fn new() -> Self;
- fn sodoku(&mut self);
- }
- impl Mob for MammalType {
- fn new() -> MammalType {
- MammalType { hp: 10, hunger: 0 }
- }
- fn sodoku(&mut self) {
- self.hp = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement