]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/class-separate-impl.rs
Replace all ~"" with "".to_owned()
[rust.git] / src / test / run-pass / class-separate-impl.rs
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 use std::fmt;
12
13 struct cat {
14     meows : uint,
15
16     how_hungry : int,
17     name : ~str,
18 }
19
20 impl cat {
21     pub fn speak(&mut self) { self.meow(); }
22
23     pub fn eat(&mut self) -> bool {
24         if self.how_hungry > 0 {
25             println!("OM NOM NOM");
26             self.how_hungry -= 2;
27             return true;
28         }
29         else {
30             println!("Not hungry!");
31             return false;
32         }
33     }
34 }
35
36 impl cat {
37     fn meow(&mut self) {
38         println!("Meow");
39         self.meows += 1u;
40         if self.meows % 5u == 0u {
41             self.how_hungry += 1;
42         }
43     }
44 }
45
46 fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat {
47     cat {
48         meows: in_x,
49         how_hungry: in_y,
50         name: in_name
51     }
52 }
53
54 impl fmt::Show for cat {
55     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56         write!(f.buf, "{}", self.name)
57     }
58 }
59
60 fn print_out(thing: ~ToStr, expected: ~str) {
61   let actual = thing.to_str();
62   println!("{}", actual);
63   assert_eq!(actual, expected);
64 }
65
66 pub fn main() {
67   let nyan: ~ToStr = ~cat(0u, 2, "nyan".to_owned()) as ~ToStr;
68   print_out(nyan, "nyan".to_owned());
69 }