Now that you have an overview of enums and it's possible variants, let's have a more complex example.
In this challenge, you will create an Animal enum that demonstrates all three types of variants.
Your Task
Create an enum Animal with the following variants:
-
Unit Struct Variant:
Dog— represents a generic dog.
-
Tuple Struct Variant:
Cat(String)— represents a cat, where theStringcontains the cat's name.
-
Named Field Struct Variant:
Bird { species: String, can_fly: bool }— represents a bird with its species and whether it can fly.
Write a function describe_animal that takes a reference to an Animal and returns a String description based on the variant:
- For
Dog, return"A friendly dog.". - For
Cat(name), return"A cat named {name}.". - For
Bird { species, can_fly }, return:"A {species} that can fly."ifcan_flyis true."A {species} that cannot fly."ifcan_flyis false.
Hints
<details> <summary>Click here to reveal hints</summary>- Use the
matchstatement to destructure tuple and named field variants. - String formatting with
format!makes it easy to include dynamic values in your description.
pub enum Animal {// Define the Animal variants here}pub fn describe_animal(animal: &Animal) -> String {// Your code here...}// Example use casepub fn main() {let dog = Animal::Dog;assert_eq!(describe_animal(&dog), "A friendly dog.");let cat = Animal::Cat("Whiskers".to_string());assert_eq!(describe_animal(&cat), "A cat named Whiskers.");let bird = Animal::Bird {species: "Penguin".to_string(),can_fly: false,};assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}