]> git.lizzy.rs Git - rust.git/blob - src/test/ui/structs-enums/class-separate-impl.rs
Rollup merge of #105674 - estebank:iterator-chains, r=oli-obk
[rust.git] / src / test / ui / structs-enums / class-separate-impl.rs
1 // run-pass
2 #![allow(dead_code)]
3 #![allow(non_camel_case_types)]
4
5 use std::fmt;
6
7 struct cat {
8     meows : usize,
9
10     how_hungry : isize,
11     name : String,
12 }
13
14 impl cat {
15     pub fn speak(&mut self) { self.meow(); }
16
17     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 cat {
31     fn meow(&mut self) {
32         println!("Meow");
33         self.meows += 1;
34         if self.meows % 5 == 0 {
35             self.how_hungry += 1;
36         }
37     }
38 }
39
40 fn cat(in_x : usize, in_y : isize, in_name: String) -> cat {
41     cat {
42         meows: in_x,
43         how_hungry: in_y,
44         name: in_name
45     }
46 }
47
48 impl fmt::Display for cat {
49     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50         write!(f, "{}", self.name)
51     }
52 }
53
54 fn print_out(thing: Box<dyn ToString>, expected: String) {
55   let actual = (*thing).to_string();
56   println!("{}", actual);
57   assert_eq!(actual.to_string(), expected);
58 }
59
60 pub fn main() {
61   let nyan: Box<dyn ToString> = Box::new(cat(0, 2, "nyan".to_string())) as Box<dyn ToString>;
62   print_out(nyan, "nyan".to_string());
63 }