2014-02-07 19:08:32 +00:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-11 01:32:48 +00:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-02-07 19:08:32 +00:00
|
|
|
// ignore-fast
|
2012-09-18 22:52:21 +00:00
|
|
|
|
2012-07-31 17:27:51 +00:00
|
|
|
trait noisy {
|
2013-08-01 22:35:46 +00:00
|
|
|
fn speak(&mut self);
|
2012-04-11 23:18:00 +00:00
|
|
|
}
|
|
|
|
|
2013-07-02 19:47:32 +00:00
|
|
|
#[deriving(Clone)]
|
2012-09-08 02:04:40 +00:00
|
|
|
struct cat {
|
2014-01-24 19:02:03 +00:00
|
|
|
meows : uint,
|
2012-09-08 02:04:40 +00:00
|
|
|
|
2013-02-25 23:14:33 +00:00
|
|
|
how_hungry : int,
|
|
|
|
name : ~str,
|
2012-09-08 02:04:40 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 22:17:22 +00:00
|
|
|
impl cat {
|
2013-02-25 23:14:33 +00:00
|
|
|
fn meow(&mut self) {
|
2013-10-21 20:08:31 +00:00
|
|
|
error!("Meow");
|
2013-02-25 23:14:33 +00:00
|
|
|
self.meows += 1u;
|
|
|
|
if self.meows % 5u == 0u {
|
|
|
|
self.how_hungry += 1;
|
|
|
|
}
|
2012-04-11 23:18:00 +00:00
|
|
|
}
|
2012-09-08 02:04:40 +00:00
|
|
|
}
|
2012-04-11 23:18:00 +00:00
|
|
|
|
2013-05-31 22:17:22 +00:00
|
|
|
impl cat {
|
|
|
|
pub fn eat(&mut self) -> bool {
|
2013-02-25 23:14:33 +00:00
|
|
|
if self.how_hungry > 0 {
|
2013-10-21 20:08:31 +00:00
|
|
|
error!("OM NOM NOM");
|
2013-02-25 23:14:33 +00:00
|
|
|
self.how_hungry -= 2;
|
|
|
|
return true;
|
|
|
|
} else {
|
2013-10-21 20:08:31 +00:00
|
|
|
error!("Not hungry!");
|
2013-02-25 23:14:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-11 23:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 19:47:00 +00:00
|
|
|
impl noisy for cat {
|
2013-08-01 22:35:46 +00:00
|
|
|
fn speak(&mut self) { self.meow(); }
|
2012-09-08 02:04:40 +00:00
|
|
|
}
|
|
|
|
|
2012-09-05 22:58:43 +00:00
|
|
|
fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat {
|
|
|
|
cat {
|
|
|
|
meows: in_x,
|
|
|
|
how_hungry: in_y,
|
2013-03-15 22:27:15 +00:00
|
|
|
name: in_name.clone()
|
2012-09-05 22:58:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-25 23:14:33 +00:00
|
|
|
fn make_speak<C:noisy>(mut c: C) {
|
2012-05-24 13:20:46 +00:00
|
|
|
c.speak();
|
|
|
|
}
|
|
|
|
|
2013-02-02 03:43:17 +00:00
|
|
|
pub fn main() {
|
2013-08-01 22:35:46 +00:00
|
|
|
let mut nyan = cat(0u, 2, ~"nyan");
|
|
|
|
nyan.eat();
|
|
|
|
assert!((!nyan.eat()));
|
2013-08-03 16:45:23 +00:00
|
|
|
for _ in range(1u, 10u) {
|
2013-08-01 22:35:46 +00:00
|
|
|
make_speak(nyan.clone());
|
|
|
|
}
|
2012-09-18 22:52:21 +00:00
|
|
|
}
|