]> git.lizzy.rs Git - rust.git/blob - src/test/ui/structs-enums/class-implement-traits.rs
Rollup merge of #89468 - FabianWolff:issue-89358, r=jackh726
[rust.git] / src / test / ui / structs-enums / class-implement-traits.rs
1 // run-pass
2 #![allow(non_camel_case_types)]
3 #![allow(dead_code)]
4
5 trait noisy {
6     fn speak(&mut self);
7 }
8
9 #[derive(Clone)]
10 struct cat {
11     meows : usize,
12
13     how_hungry : isize,
14     name : String,
15 }
16
17 impl cat {
18     fn meow(&mut self) {
19         println!("Meow");
20         self.meows += 1_usize;
21         if self.meows % 5_usize == 0_usize {
22             self.how_hungry += 1;
23         }
24     }
25 }
26
27 impl cat {
28     pub fn eat(&mut self) -> bool {
29         if self.how_hungry > 0 {
30             println!("OM NOM NOM");
31             self.how_hungry -= 2;
32             return true;
33         } else {
34             println!("Not hungry!");
35             return false;
36         }
37     }
38 }
39
40 impl noisy for cat {
41     fn speak(&mut self) { self.meow(); }
42 }
43
44 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.clone()
49     }
50 }
51
52
53 fn make_speak<C:noisy>(mut c: C) {
54     c.speak();
55 }
56
57 pub fn main() {
58     let mut nyan = cat(0_usize, 2, "nyan".to_string());
59     nyan.eat();
60     assert!((!nyan.eat()));
61     for _ in 1_usize..10_usize {
62         make_speak(nyan.clone());
63     }
64 }