blob: e5220b15520a7c95c1e351cd16006db7ecb209d4 [file] [log] [blame]
Graydon Hoared1affff2012-12-11 01:32:481// 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 Anderson3ab4b012012-08-16 01:46:5511struct cat {
Patrick Waltona08eda42013-02-25 23:14:3312 priv meows : uint,
Tim Chevalierf3343b32012-02-01 03:30:4013
Patrick Waltona08eda42013-02-25 23:14:3314 how_hungry : int,
15 name : ~str,
Brian Anderson93d3b8a2012-09-08 02:04:4016}
17
Patrick Walton5fb25462013-05-31 22:17:2218impl cat {
19 pub fn speak(&mut self) { self.meow(); }
Tim Chevalierf3343b32012-02-01 03:30:4020
Patrick Walton5fb25462013-05-31 22:17:2221 pub fn eat(&mut self) -> bool {
Patrick Waltona08eda42013-02-25 23:14:3322 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 Chevalierf3343b32012-02-01 03:30:4030 }
Tim Chevalieraae14e32012-03-24 06:08:4131}
32
Patrick Walton5fb25462013-05-31 22:17:2233impl cat {
Patrick Waltona08eda42013-02-25 23:14:3334 fn meow(&mut self) {
35 error!("Meow");
36 self.meows += 1u;
37 if self.meows % 5u == 0u {
38 self.how_hungry += 1;
39 }
Brian Anderson93d3b8a2012-09-08 02:04:4040 }
41}
42
Brian Andersonb4e547d2012-09-05 22:58:4343fn 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 Hoare89c8ef72013-02-02 03:43:1751pub fn main() {
Patrick Waltona08eda42013-02-25 23:14:3352 let mut nyan = cat(0u, 2, ~"nyan");
Tim Chevalieraae14e32012-03-24 06:08:4153 nyan.eat();
Patrick Walton1e915952013-03-29 01:39:0954 assert!((!nyan.eat()));
Daniel Micay100894552013-08-03 16:45:2355 for _ in range(1u, 10u) { nyan.speak(); };
Patrick Walton1e915952013-03-29 01:39:0956 assert!((nyan.eat()));
Patrick Walton91436882013-02-14 19:47:0057}