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.
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 the String
contains 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:
Dog
, return "A friendly dog."
.Cat(name)
, return "A cat named {name}."
.Bird { species, can_fly }
, return:
"A {species} that can fly."
if can_fly
is true."A {species} that cannot fly."
if can_fly
is false.match
statement to destructure tuple and named field variants.format!
makes it easy to include dynamic values in your description.pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_owned(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird { species, can_fly } => { let verb = if *can_fly { "can" } else { "cannot" }; format!("A {species} that {verb} fly.") }, }}// 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.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => { "A friendly dog.".to_string() }, Animal::Cat(name) => { format!("A cat named {}.", name) } Animal::Bird{species, can_fly} => { if *can_fly { format!("A {} that can fly.", species) } else { format!("A {} that cannot fly.", species) } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), // Animal::Bird{species, can_fly} => match can_fly { // true => format!("A {} that can fly.", species), // false => format!("A {} that cannot fly.", species), // } Animal::Bird{species, can_fly} => { if *can_fly { format!("A {} that can fly.", species) } else { format!("A {} that cannot fly.", species) } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird {species: String, can_fly: bool}}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => format!("A friendly dog."), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird {species, can_fly} => { if *can_fly { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), // cat's name Bird { species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal{ Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.",name), Animal::Bird{ species, can_fly } => { if *can_fly == true {format!("A {} that can fly.", species) }else{ format!("A {} that cannot fly.", species) } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species : String, can_fly : bool }}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird{species, can_fly} => { if *can_fly { format!("A {} that can fly.", species) } else { format!("A {} that cannot fly.", species) } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool, }}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird {species, can_fly } => { if *can_fly { format!("A {} that can fly.", species) } else { format!("A {} that cannot fly.", species) } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird{species, can_fly} => { if *can_fly { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } }, }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird{species: String, can_fly: bool},}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird{species, can_fly} => { if *can_fly { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } }, }}// 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.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird { species, can_fly } => format!("A {} that {} fly.", species, { if *can_fly { "can" } else { "cannot" } } ) }}// 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.");}
// Define the Animal enum with the specified variantspub enum Animal { Dog, // Unit struct variant Cat(String), // Tuple struct variant Bird { species: String, can_fly: bool }, // Named field struct variant}// Function to return a description of the animalpub fn describe_animal(animal: &Animal) -> String { // Match the Animal variant and return the corresponding description match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird { species, can_fly } => { if *can_fly { format!("A {} that can fly.", species) } else { format!("A {} that cannot fly.", species) } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird{ species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => { "A friendly dog.".to_string() } Animal::Cat(name) => { format!("A cat named {name}.") } Animal::Bird{ species, can_fly } => { if *can_fly { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } } }}// 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.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}.").to_string(), Animal::Bird{species, can_fly} => { if *can_fly { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } },}}// 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.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird { species, can_fly } => { match can_fly { true => format!("A {} that can fly.", species), false => format!("A {} that cannot fly.", species), } }, }}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird{species, can_fly} => { if *can_fly { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } }, }}// 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.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}.").to_string(), Animal::Bird{species, can_fly} => { if *can_fly { format!("A {species} that can fly.").to_string() } else { format!("A {species} that cannot fly.").to_string() } }, }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird{species, can_fly} => { if *can_fly { format!("A {species} that can fly.").to_string() } else { format!("A {species} that cannot fly.").to_string() } } }}// 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.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool}}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => String::from("A friendly dog."), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird { species, can_fly } => { match can_fly { true => format!("A {species} that can fly."), false => format!("A {species} that cannot fly.") } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird{species: String, can_fly: bool}}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => String::from("A friendly dog."), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird{species, can_fly} => { match can_fly { true => format!("A {} that can fly.", species), false => format!("A {} that cannot fly.", species), } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.",name), Animal::Bird { species, can_fly } => { if *can_fly { format!("A {} that can fly.",species) } else { format!("A {} that cannot fly.",species) } } }}// 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.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird {species, can_fly} => match can_fly { true => format!("A {} that can fly.", species), false => format!("A {} that cannot fly.", species), } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird { species, can_fly } => { if *can_fly { format!("A {} that can fly.", species) } else { format!("A {} that cannot fly.", species) } } }}// 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.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool, }}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".into(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird { species, can_fly } if *can_fly => format!("A {species} that can fly."), Animal::Bird { species, can_fly: _ } => format!("A {species} that cannot fly."), }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird { species, can_fly } => { if *can_fly { format!("A {} that can fly.", species).to_string() } else { format!("A {} that cannot fly.", species).to_string() } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird { species, can_fly } => { if can_fly.to_owned() { format!("A {} that can fly.", species).to_string() } else { format!("A {} that cannot fly.", species).to_string() } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird{species: String, can_fly: bool},}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_owned(), Animal::Cat(name) => format!("A cat named {}.", name).to_owned(), Animal::Bird{species, can_fly} => { if *can_fly == true { format!("A {} that can fly.", species).to_owned() } else { format!("A {} that cannot fly.", species).to_owned() } }, }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird{species: String, can_fly: bool}}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird{species, can_fly} => { if *can_fly { format!("A {} that can fly.", species) } else { format!("A {} that cannot fly.", species) } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool}}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal{ Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.",name), Animal::Bird{species,can_fly} => { if *can_fly { format!("A {} that can fly.",species) } else{ format!("A {} that cannot fly.",species) } } }}// 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.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird {species, can_fly } => if *can_fly { format!("A {} that can fly.", species) } else { format!("A {} that cannot fly.", species) }, }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool } }pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird {can_fly, species} => { if *can_fly { return format!("A {species} that can fly."); } format!("A {species} that cannot fly.") } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool } }pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird {can_fly: can_fly, species: species} => { if *can_fly { return format!("A {0} that can fly.", species); } format!("A {0} that cannot fly.", species) } }}// 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.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird { species, can_fly } => if *can_fly { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } }}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => String::from("A friendly dog."), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird { species, can_fly } => { if *can_fly { format!("A {} that can fly.", *species) } else { format!("A {} that cannot fly.", *species) } } }}// 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.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird { species, can_fly } => { if *can_fly { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool}}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird {species, can_fly} =>{ if *can_fly { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } } }}// 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.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird{species, can_fly} => { if *can_fly { format!("A {} that can fly.", species) } else { format!("A {} that cannot fly.", species) } } }}// 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.");}
pub enum Animal { Dog, Cat(String), Bird {species: String, can_fly: bool},}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => return "A friendly dog.".into(), Animal::Cat(name) => return format!("A cat named {name}.").into(), Animal::Bird{species, can_fly} => { let ability = if *can_fly {""} else {"not"}; return format!("A {species} that can{ability} fly.") } }}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird {species: String, can_fly: bool}}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird {species, can_fly} => { if *can_fly { return format!("A {species} that can fly.") } else { return format!("A {species} that cannot fly.") } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird {species: String, can_fly: bool}}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird {species, can_fly} => { if *can_fly { return format!("A {species} that can fly.") } else { return format!("A {species} that cannot fly.") } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(cat_name) => format!("A cat named {}.", cat_name), Animal::Bird { species, can_fly } => { format!( "A {} that {} fly.", species, if *can_fly { "can" } else { "cannot" } ) } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird { species, can_fly } => if *can_fly {format!("A {species} that can fly.")} else {format!("A {species} that cannot fly.")} }}// 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.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool, }}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird { species, can_fly } => { if *can_fly { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } } }}// 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.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird { species, can_fly } => { if *can_fly { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } } }}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool, },}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird{ species, can_fly } => { let can_cannot_fly = if *can_fly { "can" } else { "cannot" }; format!("A {species} that {can_cannot_fly} fly.") } }}// 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.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool, },}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird{ species, can_fly } => { match can_fly { true => format!("A {species} that can fly."), false => format!("A {species} that cannot fly."), } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird{species:String, can_fly:bool}}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal{ Animal::Dog => String::from("A friendly dog."), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird{species, can_fly} => { let to_fly = if *can_fly {"can"} else {"cannot"} ; format!("A {} that {} fly.",species, to_fly) } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird{species:String, can_fly:bool}}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal{ Animal::Dog => String::from("A friendly dog."), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird{species, can_fly} => { if *can_fly { return format!("A {} that can fly.",species) } format!("A {} that cannot fly.",species) } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool},}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => String::from("A friendly dog."), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird{species, can_fly} => { if *can_fly { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal{ Animal::Dog=>"A friendly dog.".to_string(), Animal::Cat(name)=>format!("A cat named {name}."), Animal::Bird{ species, can_fly }=>{ if *can_fly{ format!("A {species} that can fly.") }else{ format!("A {species} that cannot fly.") } } }}// 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.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool, }}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird { species, can_fly } => { if *can_fly { return format!("A {species} that can fly."); } return format!("A {species} that cannot fly."); } }}// 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.");}