]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
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 | ||
b7449926 XL |
11 | // run-pass |
12 | #![allow(non_camel_case_types)] | |
13 | ||
1a4d82fc JJ |
14 | #![feature(box_syntax)] |
15 | ||
16 | use std::fmt; | |
17 | ||
223e47cc | 18 | struct cat { |
c34b1796 | 19 | meows : usize, |
223e47cc | 20 | |
c34b1796 | 21 | how_hungry : isize, |
1a4d82fc | 22 | name : String, |
223e47cc LB |
23 | } |
24 | ||
970d7e83 LB |
25 | impl cat { |
26 | pub fn speak(&mut self) { self.meow(); } | |
223e47cc | 27 | |
970d7e83 | 28 | pub fn eat(&mut self) -> bool { |
223e47cc | 29 | if self.how_hungry > 0 { |
1a4d82fc | 30 | println!("OM NOM NOM"); |
223e47cc LB |
31 | self.how_hungry -= 2; |
32 | return true; | |
33 | } | |
34 | else { | |
1a4d82fc | 35 | println!("Not hungry!"); |
223e47cc LB |
36 | return false; |
37 | } | |
38 | } | |
39 | } | |
40 | ||
970d7e83 | 41 | impl cat { |
223e47cc | 42 | fn meow(&mut self) { |
1a4d82fc | 43 | println!("Meow"); |
c34b1796 AL |
44 | self.meows += 1; |
45 | if self.meows % 5 == 0 { | |
223e47cc LB |
46 | self.how_hungry += 1; |
47 | } | |
48 | } | |
49 | } | |
50 | ||
c34b1796 | 51 | fn cat(in_x : usize, in_y : isize, in_name: String) -> cat { |
223e47cc LB |
52 | cat { |
53 | meows: in_x, | |
54 | how_hungry: in_y, | |
55 | name: in_name | |
56 | } | |
57 | } | |
58 | ||
c34b1796 | 59 | impl fmt::Display for cat { |
1a4d82fc JJ |
60 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
61 | write!(f, "{}", self.name) | |
223e47cc LB |
62 | } |
63 | } | |
64 | ||
1a4d82fc JJ |
65 | fn print_out(thing: Box<ToString>, expected: String) { |
66 | let actual = (*thing).to_string(); | |
67 | println!("{}", actual); | |
68 | assert_eq!(actual.to_string(), expected); | |
223e47cc LB |
69 | } |
70 | ||
71 | pub fn main() { | |
c34b1796 | 72 | let nyan: Box<ToString> = box cat(0, 2, "nyan".to_string()) as Box<ToString>; |
1a4d82fc | 73 | print_out(nyan, "nyan".to_string()); |
223e47cc | 74 | } |