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 { // 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.".into(), 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 {} 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 { // 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 { // Define the Animal variants here Dog, Cat(String), Bird{ species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { // Your code here... use crate::Animal::*; match animal { Dog => "A friendly dog.".to_string(), Cat(name) => format!("A cat named {name}."), 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 struct Bird { species: String, can_fly: bool,}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => format!("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); }else{ return 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 { 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(x) => format!("A cat named {x}."), 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 {} that cannot fly.", species); } return format!("A {} that can 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 {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 { Dog, Cat(String), Bird { species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { return 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 { return format!("A {} that can fly.", species).to_string() } return 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 => format!("A friendly dog."), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird{species, can_fly} => { format!("A {species} that can{} fly.", if *can_fly {""} else {"not"}) } }}// 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} => { format!("A {species} that can{} fly.", if *can_fly {""} else {"not"}) } }}// 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} => { 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 => "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 => "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) => { String::from(format!("A cat named {name}.")) } Animal::Bird{ species, can_fly} => { if *can_fly{ String::from(format!("A {species} that can fly.")) } else{ String::from(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 he 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 == false { format!("A {} that cannot fly.", species) } else { format!("A {} that can 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 => { return format!("A friendly dog.")}, Animal::Bird{species, can_fly } => { if *can_fly { return format!("A {species} that can fly.") } else{ return format!("A {species} that cannot fly.") } }, Animal::Cat(name) => { return format!("A cat named {name}.") } }}// 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).to_string(), Animal::Bird { species, can_fly } => match can_fly { true => format!("A {} that can fly.", species).to_string(), false => 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 { 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 => String::from("A friendly dog."), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird{species, can_fly: true} => format!("A {species} that can fly."), Animal::Bird{species, 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 => "A friendly dog.".to_string(), Animal::Cat(name) => "A cat named ".to_string() + name + ".", Animal::Bird { species, can_fly } => if *can_fly { "A ".to_string() + &species + " that can fly." } else { "A ".to_string() + &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: true} => format!("A {species} that can fly."), Animal::Bird{species, 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 => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird { species, can_fly: true } => { format!("A {} that can fly.", species) }, Animal::Bird { species, can_fly: 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 {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 == true { 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 => "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}.").to_string(), Animal::Bird{species, can_fly} => format!("A {species} that {} fly.", 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 { Dog, Cat(String), Bird { species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { 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), 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(text) => format!("A cat named {}.", text), 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 { 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::Bird { species, can_fly } => { if *can_fly { format!("A {} that can fly.", species) } else { format!("A {} that cannot fly.", species) } }, Animal::Cat(name) => format!("A cat named {}.", name) }}// 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: true} => format!("A {} that can fly.", species), Animal::Bird {species, can_fly: 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 { 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: true } => format!("A {} that can fly.", species), Animal::Bird { species, can_fly: 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 { 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} => { let c = if *can_fly {"can"} else {"cannot"}; format!("A {species} that {c} 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: true } => format!("A {} that can fly.", species), Animal::Bird { species, can_fly: 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 { 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 } => 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 {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 => "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(c) => format!("A cat named {c}."), 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 {}.",species,if *can_fly {"can fly"} else {"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(description) => format!("A cat named {description}."), Animal::Bird{species, can_fly:true} => format!("A {species} that can fly."), Animal::Bird{species, 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 => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird{ species, can_fly } => format!("A {species} that can{} fly.", (if *can_fly {""} else {"not"})), }}// 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.".to_string(), Animal::Cat(name) => return format!("A cat named {name}.").to_string(), Animal::Bird{species, can_fly } if *can_fly => return format!("A {species} that can fly."), Animal::Bird{species, 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.");}
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.".to_string(), Animal::Cat(name) => return format!("A cat named {name}.").to_string(), Animal::Bird{species, can_fly } if *can_fly => return format!("A {species} that can fly."), Animal::Bird{species, 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.");}
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 {} that can fly.", species) } return 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 => return "A friendly dog.".to_string(), Animal::Cat(n) => return format!("A cat named {}.", n), 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... return 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.");}