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}=>{ let mut ex = ""; if !can_fly {ex="not"} format!("A {} that can{} fly.", species,ex) } }}// 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 mut ex = ""; if !can_fly {ex="not"} format!("A {} that can{} fly.", species,ex) } }}// 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(cat_name) => { let ret_val = format!("A cat named {}.", cat_name); return String::from(ret_val); } Animal::Bird{species, can_fly} => { if *can_fly{ return format!("A {} that can fly.", species).to_string(); } else{ return format!("A {} that cannot fly.", species).to_string(); } } _ => {return "".to_string();} }}// Example use casepub fn main() { let dog = Animal::Dog; assert_eq!(describe_animal(&dog), "A friendly dog."); let cat = Animal::Cat("Whiskers".to_string()); assert_eq!(describe_animal(&cat), "A cat named Whiskers."); let bird = Animal::Bird { species: "Penguin".to_string(), can_fly: false, }; assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird {species, can_fly} => if *can_fly { format!("A {species} that can fly.") } 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(n)=>format!("A cat named {n}."), Animal::Bird{species,can_fly}=>{ let a = if *can_fly { "can fly" }else{ "cannot fly" }; format!("A {species} that {a}.")} }}// 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 {} 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 } => { let verb = if *can_fly { "can" } else { "cannot" }; format!("A {} that {} fly.", species, verb) }, }}// 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 verb = if *can_fly { "can" } else { "cannot" }; format!("A {} that {} fly.", species, verb) }, }}// 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.").to_string(), false => format!("A {species} that cannot fly.").to_string(), } } }}// Example use casepub fn main() { let dog = Animal::Dog; assert_eq!(describe_animal(&dog), "A friendly dog."); let cat = Animal::Cat("Whiskers".to_string()); assert_eq!(describe_animal(&cat), "A cat named Whiskers."); let bird = Animal::Bird { species: "Penguin".to_string(), can_fly: false, }; assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird{ species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => String::from("A friendly dog."), Animal::Cat(name) => String::from(format!("A cat named {name}.")), Animal::Bird{species, can_fly} => { match can_fly { true => String::from(format!("A {species} that can fly.")), false => 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 { 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 == &true { format!("A {} that can fly.", species) } else{ format!("A {} that cannot fly.", species) } }, }}// Example use casepub fn main() { let dog = Animal::Dog; assert_eq!(describe_animal(&dog), "A friendly dog."); let cat = Animal::Cat("Whiskers".to_string()); assert_eq!(describe_animal(&cat), "A cat named Whiskers."); let bird = Animal::Bird { species: "Penguin".to_string(), can_fly: false, }; assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird {species: String, can_fly: bool},}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => return String::from("A friendly dog."), Animal::Cat(s) => return String::from(format!("A cat named {}.", s)), Animal::Bird{ species, can_fly } => { match can_fly { true => String::from(format!("A {} that can fly.", species)), false => String::from(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 { 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::Bird{species, can_fly} => if *can_fly {format!("A {species} that can fly.")} else {format!("A {species} that cannot fly.")}, Animal::Cat(name) => format!("A cat named {name}."), _ => "A friendly dog.".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 { return 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 ==true{ format!("A {} that can fly.", species) } else{ format!("A {} that cannot fly.",species) } } }}// Example use casepub fn main() { let dog = Animal::Dog; assert_eq!(describe_animal(&dog), "A friendly dog."); let cat = Animal::Cat("Whiskers".to_string()); assert_eq!(describe_animal(&cat), "A cat named Whiskers."); let bird = Animal::Bird { species: "Penguin".to_string(), can_fly: false, }; assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird{ species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird{species, can_fly} => { if *can_fly { format!("A {} that can fly.", species) } else { format!("A {} that cannot fly.", species) } }, }}// Example use casepub fn main() { let dog = Animal::Dog; assert_eq!(describe_animal(&dog), "A friendly dog."); let cat = Animal::Cat("Whiskers".to_string()); assert_eq!(describe_animal(&cat), "A cat named Whiskers."); let bird = Animal::Bird { species: "Penguin".to_string(), can_fly: false, }; assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool },}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => String::from("A friendly dog."), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird { species, can_fly } => match can_fly { true => format!("A {species} that can fly."), false => format!("A {species} that cannot fly."), }, }}// Example use casepub fn main() { let dog = Animal::Dog; assert_eq!(describe_animal(&dog), "A friendly dog."); let cat = Animal::Cat("Whiskers".to_string()); assert_eq!(describe_animal(&cat), "A cat named Whiskers."); let bird = Animal::Bird { species: "Penguin".to_string(), can_fly: false, }; assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}
pub enum Animal { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool } }pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird { species, can_fly } => { if *can_fly { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } } }}// Example use casepub fn main() { let dog = Animal::Dog; assert_eq!(describe_animal(&dog), "A friendly dog."); let cat = Animal::Cat("Whiskers".to_string()); assert_eq!(describe_animal(&cat), "A cat named Whiskers."); let bird = Animal::Bird { species: "Penguin".to_string(), can_fly: false, }; assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}
pub enum Animal { Dog, Cat(String), Bird { species : String, can_fly : bool }}pub fn describe_animal(animal : &Animal) -> String{ match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird {species ,can_fly} => { if *can_fly { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } } }}// Example use casepub fn main() { let dog = Animal::Dog; assert_eq!(describe_animal(&dog), "A friendly dog."); let cat = Animal::Cat("Whiskers".to_string()); assert_eq!(describe_animal(&cat), "A cat named Whiskers."); let bird = Animal::Bird { species: "Penguin".to_string(), can_fly: false, }; assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}
pub enum Animal { Dog, Cat(String), Bird{species:String, can_fly: bool}}pub fn describe_animal(animal: &Animal) -> String { match animal{ Animal::Dog => String::from("A friendly dog."), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird{species, can_fly} => match can_fly{ true => format!("A {species} that can fly."), false => format!("A {species} that cannot fly."), } }}// Example use casepub fn main() { let dog = Animal::Dog; assert_eq!(describe_animal(&dog), "A friendly dog."); let cat = Animal::Cat("Whiskers".to_string()); assert_eq!(describe_animal(&cat), "A cat named Whiskers."); let bird = Animal::Bird { species: "Penguin".to_string(), can_fly: false, }; assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}
pub enum Animal { 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 can{} fly.", if !*can_fly {"not"} else { "" }) }, }}// 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(cat) => format!("A cat named {}.", cat), Animal::Bird{ species, can_fly} => format!("A {} that can{} fly.", species, if !*can_fly {"not"} else { "" }), }}// 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 {species} that can{} fly.", if !*can_fly { "not" } else {""}), }}// Example use casepub fn main() { let dog = Animal::Dog; assert_eq!(describe_animal(&dog), "A friendly dog."); let cat = Animal::Cat("Whiskers".to_string()); assert_eq!(describe_animal(&cat), "A cat named Whiskers."); let bird = Animal::Bird { species: "Penguin".to_string(), can_fly: false, }; assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}
pub enum Animal { Dog, Cat(String), Bird{species: String, can_fly: bool}}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".into(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird{species, can_fly: 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.");}
#![allow(dead_code)]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 { 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(x) => format!("A cat named {x}."), Animal::Bird{ species, can_fly } => format!("A {species} that {}.", 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 { // 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 start = format!("A {} that", species); if *can_fly { return format!("{} can fly.", start); } else { return format!("{} cannot fly.", start); } } }}// 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} => { let mut can_cannot = ""; if !can_fly { can_cannot = "not"; } format!("A {} that can{} fly.", species, can_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 { // Define the Animal variants here Dog, Cat(String), Bird { species: String, can_fly: bool }}pub fn describe_animal(animal: &Animal) -> String { // Your code here... match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird { species, can_fly } => { if *can_fly { format!("A {} that can fly.", species) } else { format!("A {} that cannot fly.", species) } } }}// Example use casepub fn main() { let dog = Animal::Dog; assert_eq!(describe_animal(&dog), "A friendly dog."); let cat = Animal::Cat("Whiskers".to_string()); assert_eq!(describe_animal(&cat), "A cat named Whiskers."); let bird = Animal::Bird { species: "Penguin".to_string(), can_fly: false, }; assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}
pub enum Animal { Dog, Cat(String), Bird{species:String,can_fly:bool},}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird{species, can_fly} => { if *can_fly { format!("A {} that can fly.", species) } else { format!("A {} that cannot fly.", species) } } }}// Example use casepub fn main() { let dog = Animal::Dog; assert_eq!(describe_animal(&dog), "A friendly dog."); let cat = Animal::Cat("Whiskers".to_string()); assert_eq!(describe_animal(&cat), "A cat named Whiskers."); let bird = Animal::Bird { species: "Penguin".to_string(), can_fly: false, }; assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}
pub enum Animal { Dog, Cat(String), Bird { species: String, can_fly: bool, }}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird {species, can_fly } => { let verb: String; match can_fly { true => { verb = "can".to_string(); }, false => { verb = "cannot".to_string(); }, } format!("A {} that {} fly.", species, verb) } }}// 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 } => { let can_fly_verb: String; if *can_fly { can_fly_verb = "can".to_string(); } else { can_fly_verb = "cannot".to_string(); } format!("A {} that {} fly.", species, can_fly_verb) } }}// Example use casepub fn main() { let dog = Animal::Dog; assert_eq!(describe_animal(&dog), "A friendly dog."); let cat = Animal::Cat("Whiskers".to_string()); assert_eq!(describe_animal(&cat), "A cat named Whiskers."); let bird = Animal::Bird { species: "Penguin".to_string(), can_fly: false, }; assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}
pub enum Animal { Dog, Cat(String), Bird {species: String, can_fly: bool}}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird{ species, can_fly } => { if *can_fly { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } } }}// Example use casepub fn main() { let dog = Animal::Dog; assert_eq!(describe_animal(&dog), "A friendly dog."); let cat = Animal::Cat("Whiskers".to_string()); assert_eq!(describe_animal(&cat), "A cat named Whiskers."); let bird = Animal::Bird { species: "Penguin".to_string(), can_fly: false, }; assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}
pub enum Animal { Dog, Cat(String), Bird {species: String, can_fly: bool}}pub fn describe_animal(animal: &Animal) -> String { match animal { Animal::Dog => "A friendly dog.".to_string(), Animal::Cat(name) => format!("A cat named {name}."), Animal::Bird{ species, can_fly } => { if *can_fly { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } } }}// 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 {} 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 {} 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 == true { format!("A {} that can fly.", species) } else { format!("A {} that cannot fly.", species) } }, }}// Example use casepub fn main() { let dog = Animal::Dog; assert_eq!(describe_animal(&dog), "A friendly dog."); let cat = Animal::Cat("Whiskers".to_string()); assert_eq!(describe_animal(&cat), "A cat named Whiskers."); let bird = Animal::Bird { species: "Penguin".to_string(), can_fly: false, }; assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}
pub enum Animal { 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) => 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 => String::from("A friendly dog."), Animal::Cat(name) => format!("A cat named {}.", name), Animal::Bird{species, can_fly} => match can_fly{ true => format!("A {} that can fly.", species), false => format!("A {} that cannot fly.", species), } }}// Example use casepub fn main() { let dog = Animal::Dog; assert_eq!(describe_animal(&dog), "A friendly dog."); let cat = Animal::Cat("Whiskers".to_string()); assert_eq!(describe_animal(&cat), "A cat named Whiskers."); let bird = Animal::Bird { species: "Penguin".to_string(), can_fly: false, }; assert_eq!(describe_animal(&bird), "A Penguin that cannot fly.");}
pub enum Animal { 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} => { 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.".into(), Animal:: Cat(name) => format!("A cat named {}.",name), Animal:: Bird { species, can_fly} => { let can_fly = if *can_fly { "" } else { "not" }; format!("A {} that can{} fly.",species,can_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 == true { format!("A {species} that can fly.") } else { format!("A {species} that cannot fly.") } } }}pub 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.");}