Search⌘ K

Solution: Make Cats Meow and Dogs Bark

Understand how to implement behavior for different animal types using Rust enumerations and vector iteration. Learn to derive traits for enums, print specific sounds based on animal variants, and apply conditional logic to enrich your dungeon crawler game's interactivity.

We'll cover the following...

Solution

The complete solution to the problem is provided below. Let’s have a look at it.

Rust 1.40.0
#[derive(PartialEq)]
enum AnimalType { Cat, Dog }
fn main() {
let animals = vec![ AnimalType::Cat, AnimalType::Dog ];
for animal in animals {
if animal == AnimalType::Cat {
println!("Meow");
}
else if animal == AnimalType::Dog {
println!("Woof");
}
else
{
println!("Not an animal");
}
}
}

Explanation

...