0

I am doing rust by example book and on the second question of trait objects, I am getting an error

trait Bird {
    fn quack(&self);
}

struct Duck;
impl Duck {
    fn fly(&self) {
        println!("Look, the duck is flying")
    }
}
struct Swan;
impl Swan {
    fn fly(&self) {
        println!("Look, the duck.. oh sorry, the swan is flying")
    }
}

impl Bird for Duck {
    fn quack(&self) {
        println!("{}", "duck duck");
    }
}

impl Bird for Swan {
    fn quack(&self) {
        println!("{}", "swan swan");
    }
}

fn main() {
    // FILL in the blank to make the code work.
    let birds = [&Duck, &Swan];

    for bird in birds {
        bird.quack();
        
    }
}

This is the error:

   |
32 |     let birds = [&Duck, &Swan];
   |                         ^^^^^ expected `&Duck`, found `&Swan`
   |
   = note: expected reference `&Duck`
              found reference `&Swan`

I was expecting following line as my output: duck duck swan swan

0 Answers0