I declared an array of a custom trait Animal
in order to experiment with polymorphism in Rust, but the compiler seems to do type inference on the subtype of the first element instead:
fn main() {
let animals = [Cat, Dog, Cat, Lion, Dog, Lion];
for single_animal in animals {
single_animal.talk();
}
}
trait Animal {
fn talk(&self);
}
struct Cat;
struct Dog;
struct Lion;
impl Animal for Cat {
fn talk(&self) {
println!("Je miaule !");
}
}
impl Animal for Dog {
fn talk(&self) {
println!("J'aboie !");
}
}
impl Animal for Lion {
fn talk(&self) {
println!("Je rugit !");
}
}
The compiler complains about the fact that the first element is a Cat
and not the others:
error: mismatched types [--explain E0308]
--> src/main.rs:3:25
|>
3 |> let animals = [Cat, Dog, Cat, Lion, Dog, Lion];
|> ^^^ expected struct `Cat`, found struct `Dog`
note: expected type `Cat`
note: found type `Dog`
error: mismatched types [--explain E0308]
--> src/main.rs:3:35
|>
3 |> let animals = [Cat, Dog, Cat, Lion, Dog, Lion];
|> ^^^^ expected struct `Cat`, found struct `Lion`
note: expected type `Cat`
note: found type `Lion`
error: mismatched types [--explain E0308]
--> src/main.rs:3:41
|>
3 |> let animals = [Cat, Dog, Cat, Lion, Dog, Lion];
|> ^^^ expected struct `Cat`, found struct `Dog`
note: expected type `Cat`
note: found type `Dog`
error: mismatched types [--explain E0308]
--> src/main.rs:3:46
|>
3 |> let animals = [Cat, Dog, Cat, Lion, Dog, Lion];
|> ^^^^ expected struct `Cat`, found struct `Lion`
note: expected type `Cat`
note: found type `Lion`
error: the trait bound `[Cat; 6]: std::iter::Iterator` is not satisfied [--explain E0277]
--> src/main.rs:4:5
|>
4 |> for single_animal in animals {
|> ^
note: `[Cat; 6]` is not an iterator; maybe try calling `.iter()` or a similar method
note: required by `std::iter::IntoIterator::into_iter`
Adding Animal
type to the array does not solve the problem either. Because this time I get more errors:
error: mismatched types [--explain E0308]
--> src/main.rs:3:27
|>
3 |> let animals: Animal = [Cat, Dog, Cat, Lion, Dog, Lion];
|> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait Animal, found array of 6 elements
note: expected type `Animal`
note: found type `[Cat; 6]`
error: the trait bound `Animal: std::marker::Sized` is not satisfied [--explain E0277]
--> src/main.rs:3:9
|>
3 |> let animals: Animal = [Cat, Dog, Cat, Lion, Dog, Lion];
|> ^^^^^^^
note: `Animal` does not have a constant size known at compile-time
note: all local variables must have a statically known size
error: the trait bound `Animal: std::marker::Sized` is not satisfied [--explain E0277]
--> src/main.rs:4:5
|>
4 |> for single_animal in animals {
|> ^
note: `Animal` does not have a constant size known at compile-time
note: required by `std::iter::IntoIterator::into_iter`
error: the trait bound `Animal: std::iter::Iterator` is not satisfied [--explain E0277]
--> src/main.rs:4:5
|>
4 |> for single_animal in animals {
|> ^
note: `Animal` is not an iterator; maybe try calling `.iter()` or a similar method
note: required by `std::iter::IntoIterator::into_iter`