1 // Copyright 2012-2014 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 #![allow(non_camel_case_types)]
12
13 #[cfg(feature = "use_core")]
14 extern crate core;
15
16 #[macro_use]
17 extern crate derivative;
18
19 trait noisy {
speak(&mut self)20 fn speak(&mut self);
21 }
22
23 #[derive(Derivative)]
24 #[derivative(Clone)]
25 struct cat {
26 meows : usize,
27
28 how_hungry : isize,
29 name : String,
30 }
31
32 impl cat {
meow(&mut self)33 fn meow(&mut self) {
34 println!("Meow");
35 self.meows += 1_usize;
36 if self.meows % 5_usize == 0_usize {
37 self.how_hungry += 1;
38 }
39 }
40 }
41
42 impl cat {
eat(&mut self) -> bool43 pub fn eat(&mut self) -> bool {
44 if self.how_hungry > 0 {
45 println!("OM NOM NOM");
46 self.how_hungry -= 2;
47 true
48 } else {
49 println!("Not hungry!");
50 false
51 }
52 }
53 }
54
55 impl noisy for cat {
speak(&mut self)56 fn speak(&mut self) { self.meow(); }
57 }
58
cat(in_x : usize, in_y : isize, in_name: String) -> cat59 fn cat(in_x : usize, in_y : isize, in_name: String) -> cat {
60 cat {
61 meows: in_x,
62 how_hungry: in_y,
63 name: in_name,
64 }
65 }
66
67
make_speak<C:noisy>(mut c: C)68 fn make_speak<C:noisy>(mut c: C) {
69 c.speak();
70 }
71
72 #[test]
main()73 fn main() {
74 let mut nyan = cat(0_usize, 2, "nyan".to_string());
75 nyan.eat();
76 assert!((!nyan.eat()));
77 for _ in 1_usize..10_usize {
78 make_speak(nyan.clone());
79 }
80 }
81