Complex Enums
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
Click here to reveal hints
- Use the
matchstatement to destructure tuple and named field variants. - String formatting with
format!makes it easy to include dynamic values in your description.
No run yet
Press
Run to execute your code and see test results here.