1 // run-pass
2 #![allow(dead_code)]
3 #![allow(non_camel_case_types)]
4
5 // aux-build:cci_class_trait.rs
6 extern crate cci_class_trait;
7 use cci_class_trait::animals::noisy;
8
9 struct cat {
10 meows: usize,
11
12 how_hungry : isize,
13 name : String,
14 }
15
16 impl cat {
eat(&mut self) -> bool17 pub fn eat(&mut self) -> bool {
18 if self.how_hungry > 0 {
19 println!("OM NOM NOM");
20 self.how_hungry -= 2;
21 return true;
22 }
23 else {
24 println!("Not hungry!");
25 return false;
26 }
27 }
28 }
29
30 impl noisy for cat {
speak(&mut self)31 fn speak(&mut self) { self.meow(); }
32 }
33
34 impl cat {
meow(&mut self)35 fn meow(&mut self) {
36 println!("Meow");
37 self.meows += 1_usize;
38 if self.meows % 5_usize == 0_usize {
39 self.how_hungry += 1;
40 }
41 }
42 }
43
cat(in_x : usize, in_y : isize, in_name: String) -> cat44 fn cat(in_x : usize, in_y : isize, in_name: String) -> cat {
45 cat {
46 meows: in_x,
47 how_hungry: in_y,
48 name: in_name
49 }
50 }
51
52
main()53 pub fn main() {
54 let mut nyan = cat(0_usize, 2, "nyan".to_string());
55 nyan.eat();
56 assert!((!nyan.eat()));
57 for _ in 1_usize..10_usize { nyan.speak(); };
58 assert!((nyan.eat()));
59 }
60