Enjoy Day 4
Now we need
enum
to replace "cat" and "duck" type from previous example.
Enums
You also can
impl
to enum
👇.
💡 There's more examples about Enums, and match enums.
Don't worry about
&'static str
or lifetimes
just yet, compiler will let you know when need (usually out of { }
scope ) and we will talk about it later. Let's continue on other topics.
But I want enum string! How!
That's a great question! See below👇
strum
Install
cargo add strum
cargo add strum_macros
and get
strum = "0.26"
strum_macros = "0.26"
or
cargo add strum --features=derive
and get
strum = { version = "0.26", features = ["derive", "strum_macros"] }
🤷
strum
is not runnable via Rust Playground so output is shown below.
Run
1️⃣ animal_type: Ok(Cat)
2️⃣ animal_type: "catty"
3️⃣ cat_sound_result: Ok(Cat)
4️⃣ cat_sound_string: "meaowww"
5️⃣ my_pet: "snoopy"
Let's try some more complicated one!
Enums Variants
Awww, I'm hungry...