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}."), 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.");}
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 => format!("A friendly dog."), 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(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.") } } _ => "".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 => "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 {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 => "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_owned(), 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 { 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 } => 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 => 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."), 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 { 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); } 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{ 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 } => 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(n) => format!("A cat named {}.", n), 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 struct Dog;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 { 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).to_string(), Animal::Bird{ species, can_fly } => format!("A {} that {} fly.", species, if *can_fly { "can" } else { "cannot" }), // { // 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 {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: 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} => { let msg = if *can_fly {" fly"} else {"not fly"}; format!("A {} that can{}.", species, msg) } }}// 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} => {let msg = if *can_fly {"can fly"} else {"cannot fly"};format!{"A {} that {}.", species, msg} } }}// 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 { 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(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 { // 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}=> 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.");}