-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum.rs
More file actions
29 lines (25 loc) · 851 Bytes
/
Copy pathenum.rs
File metadata and controls
29 lines (25 loc) · 851 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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 case
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.");
}