1 pub mod kitty { 2 use std::fmt; 3 4 pub struct cat { 5 meows : usize, 6 pub how_hungry : isize, 7 pub name : String, 8 } 9 10 impl fmt::Display for cat { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result11 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 12 write!(f, "{}", self.name) 13 } 14 } 15 16 impl cat { meow(&mut self)17 fn meow(&mut self) { 18 println!("Meow"); 19 self.meows += 1; 20 if self.meows % 5 == 0 { 21 self.how_hungry += 1; 22 } 23 } 24 25 } 26 27 impl cat { speak(&mut self)28 pub fn speak(&mut self) { self.meow(); } 29 eat(&mut self) -> bool30 pub fn eat(&mut self) -> bool { 31 if self.how_hungry > 0 { 32 println!("OM NOM NOM"); 33 self.how_hungry -= 2; 34 return true; 35 } 36 else { 37 println!("Not hungry!"); 38 return false; 39 } 40 } 41 } 42 cat(in_x : usize, in_y : isize, in_name: String) -> cat43 pub fn cat(in_x : usize, in_y : isize, in_name: String) -> cat { 44 cat { 45 meows: in_x, 46 how_hungry: in_y, 47 name: in_name 48 } 49 } 50 } 51