Skip to content

Instantly share code, notes, and snippets.

@alecchendev
Last active December 24, 2020 01:04
Show Gist options
  • Save alecchendev/bb2fbe03e51f49888509da16b82b8c61 to your computer and use it in GitHub Desktop.
Save alecchendev/bb2fbe03e51f49888509da16b82b8c61 to your computer and use it in GitHub Desktop.
OOP in rust article - animal enum
enum Animal {
Dog(String),
Cat(String),
Pig(bool),
}
fn main() {
let example_animal = Animal::Pig(true);
match example_animal {
Animal::Dog(name) => println!("It's a dog named {}!", name),
Animal::Cat(color) => println!("It's a cat with {} fur!", color),
Animal::Pig(cooked) => {
if cooked {
println!("It's dinner!");
} else {
println!("It's a pig!");
}
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment