]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/classes.rs
auto merge of #13600 : brandonw/rust/master, r=brson
[rust.git] / src / test / run-pass / classes.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 struct cat {
12     meows : uint,
13
14     how_hungry : int,
15     name : ~str,
16 }
17
18 impl cat {
19     pub fn speak(&mut self) { self.meow(); }
20
21     pub fn eat(&mut self) -> bool {
22         if self.how_hungry > 0 {
23             println!("OM NOM NOM");
24             self.how_hungry -= 2;
25             return true;
26         } else {
27             println!("Not hungry!");
28             return false;
29         }
30     }
31 }
32
33 impl cat {
34     fn meow(&mut self) {
35         println!("Meow");
36         self.meows += 1u;
37         if self.meows % 5u == 0u {
38             self.how_hungry += 1;
39         }
40     }
41 }
42
43 fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat {
44     cat {
45         meows: in_x,
46         how_hungry: in_y,
47         name: in_name
48     }
49 }
50
51 pub fn main() {
52   let mut nyan = cat(0u, 2, ~"nyan");
53   nyan.eat();
54   assert!((!nyan.eat()));
55   for _ in range(1u, 10u) { nyan.speak(); };
56   assert!((nyan.eat()));
57 }