]> git.lizzy.rs Git - rust.git/blob - src/test/ui/class-cast-to-trait.rs
Rollup merge of #57132 - daxpedda:master, r=steveklabnik
[rust.git] / src / test / ui / class-cast-to-trait.rs
1 #![feature(box_syntax)]
2
3 trait Noisy {
4   fn speak(&self);
5 }
6
7 struct Cat {
8   meows : usize,
9
10   how_hungry : isize,
11   name : String,
12 }
13
14 impl Cat {
15   pub fn eat(&self) -> bool {
16     if self.how_hungry > 0 {
17         println!("OM NOM NOM");
18         self.how_hungry -= 2;
19         return true;
20     }
21     else {
22         println!("Not hungry!");
23         return false;
24     }
25   }
26 }
27
28 impl Noisy for Cat {
29   fn speak(&self) { self.meow(); }
30
31 }
32
33 impl Cat {
34     fn meow(&self) {
35       println!("Meow");
36       self.meows += 1;
37       if self.meows % 5 == 0 {
38           self.how_hungry += 1;
39       }
40     }
41 }
42
43 fn cat(in_x : usize, in_y : isize, in_name: String) -> Cat {
44     Cat {
45         meows: in_x,
46         how_hungry: in_y,
47         name: in_name
48     }
49 }
50
51 fn main() {
52   let nyan: Box<Noisy> = box cat(0, 2, "nyan".to_string()) as Box<Noisy>;
53   nyan.eat(); //~ ERROR no method named `eat` found
54 }