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.".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) } 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 { // 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 String::from("A friendly dog."), Animal::Cat(name) => return 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 => "A friendly dog.".to_string(), Animal::Cat(v) => format!("A cat named {}.", v), 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).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 { 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 { // 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 => 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 { // 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}, // Define the Animal variants here}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 flies_or_not = if *can_fly {"can fly"} else {"cannot fly"}; format!("A {species} that {flies_or_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 => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), 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 { // 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(n) => format!("A cat named {n}.").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 { 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.");}
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."), Animal::Bird{species,..} => 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 { // Your code here... match animal { Animal::Dog => return "A friendly dog.".to_string(), Animal::Cat(name) => return 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 { // 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: 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 { match animal { Animal::Dog => "A friendly dog.".into(), 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 { 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 {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 { 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 { return match animal { Animal::Dog => format!("A friendly dog."), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird{species, can_fly} => if *can_fly == true {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} => format!{"A {spec} that {fly} fly.", spec = species, fly = if *can_fly == true {"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... return match animal{ Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(t) => format!("A cat named {t}."), Animal::Bird{species : s,can_fly : b} => { if *b { format!("A {s} that can fly.") }else{ format!("A {s} 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_owned(), Animal::Cat(st) => format!("A cat named {st}."), 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 {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 { 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 {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 { 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 => String::from("A friendly dog."), Animal::Cat(s) => format!("A cat named {}.", s), 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 {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} => 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 => 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 } => 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 { // Define the Animal variants here Dog, Cat(String), Bird{species : String , can_fly : bool},}pub fn describe_animal(animal: &Animal) -> String { // Your code here... let res = 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), }; res}// 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} => { 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 { // 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{ 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 { // 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 { // 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 { 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 => 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 { 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 => 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 => 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 => 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 { 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 {} 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.");}