Enums can be cast into integers
Rust enums
will automatically cast into integers only if they do not have receive values.
The code below will not cast into integers, as we have values present
🚫
enum Gender{
Male(String),
Female(String),
}
The code below will cast into integers
✅
enum Gender{
Male,
Female,
}
We automatically have the those enum
options in integers.
enum Mood{
Sad,
Sleepy,
Happy,
Joyful
}
fn main(){
use Mood::*;
let moods = vec![Happy as u32,Sad as u32,Sleepy as u32,Joyful as u32];
let my_mood = &moods[3];
println!("on a Scale of 0 - 3, how do you feel {}", my_mood);
}