Graydon Hoare | d1affff | 2012-12-11 01:32:48 | [diff] [blame] | 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 | |
Brian Anderson | 3ab4b01 | 2012-08-16 01:46:55 | [diff] [blame] | 11 | struct cat { |
Patrick Walton | a08eda4 | 2013-02-25 23:14:33 | [diff] [blame] | 12 | priv meows : uint, |
Tim Chevalier | f3343b3 | 2012-02-01 03:30:40 | [diff] [blame] | 13 | |
Patrick Walton | a08eda4 | 2013-02-25 23:14:33 | [diff] [blame] | 14 | how_hungry : int, |
| 15 | name : ~str, |
Brian Anderson | 93d3b8a | 2012-09-08 02:04:40 | [diff] [blame] | 16 | } |
| 17 | |
Patrick Walton | 5fb2546 | 2013-05-31 22:17:22 | [diff] [blame] | 18 | impl cat { |
| 19 | pub fn speak(&mut self) { self.meow(); } |
Tim Chevalier | f3343b3 | 2012-02-01 03:30:40 | [diff] [blame] | 20 | |
Patrick Walton | 5fb2546 | 2013-05-31 22:17:22 | [diff] [blame] | 21 | pub fn eat(&mut self) -> bool { |
Patrick Walton | a08eda4 | 2013-02-25 23:14:33 | [diff] [blame] | 22 | if self.how_hungry > 0 { |
| 23 | error!("OM NOM NOM"); |
| 24 | self.how_hungry -= 2; |
| 25 | return true; |
| 26 | } else { |
| 27 | error!("Not hungry!"); |
| 28 | return false; |
| 29 | } |
Tim Chevalier | f3343b3 | 2012-02-01 03:30:40 | [diff] [blame] | 30 | } |
Tim Chevalier | aae14e3 | 2012-03-24 06:08:41 | [diff] [blame] | 31 | } |
| 32 | |
Patrick Walton | 5fb2546 | 2013-05-31 22:17:22 | [diff] [blame] | 33 | impl cat { |
Patrick Walton | a08eda4 | 2013-02-25 23:14:33 | [diff] [blame] | 34 | fn meow(&mut self) { |
| 35 | error!("Meow"); |
| 36 | self.meows += 1u; |
| 37 | if self.meows % 5u == 0u { |
| 38 | self.how_hungry += 1; |
| 39 | } |
Brian Anderson | 93d3b8a | 2012-09-08 02:04:40 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
Brian Anderson | b4e547d | 2012-09-05 22:58:43 | [diff] [blame] | 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 | |
Graydon Hoare | 89c8ef7 | 2013-02-02 03:43:17 | [diff] [blame] | 51 | pub fn main() { |
Patrick Walton | a08eda4 | 2013-02-25 23:14:33 | [diff] [blame] | 52 | let mut nyan = cat(0u, 2, ~"nyan"); |
Tim Chevalier | aae14e3 | 2012-03-24 06:08:41 | [diff] [blame] | 53 | nyan.eat(); |
Patrick Walton | 1e91595 | 2013-03-29 01:39:09 | [diff] [blame] | 54 | assert!((!nyan.eat())); |
Daniel Micay | 10089455 | 2013-08-03 16:45:23 | [diff] [blame] | 55 | for _ in range(1u, 10u) { nyan.speak(); }; |
Patrick Walton | 1e91595 | 2013-03-29 01:39:09 | [diff] [blame] | 56 | assert!((nyan.eat())); |
Patrick Walton | 9143688 | 2013-02-14 19:47:00 | [diff] [blame] | 57 | } |