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