<ao> | Adetunji's Blog

Enum can also hold data

A struct can hold an enum, an enum can hold a struct and other data types.

fn main(){
    let curr_state = create_skystate(7);
    check_skystate(&curr_state);
}

enum ThingsInTheSky {
    Sun(String),
    Moon(String),
}

fn create_skystate(time: i32) -> ThingsInTheSky{
    match time{
        6..=18 => ThingsInTheSky::Sun(String::from("I can see the sun")),
        _ =>  ThingsInTheSky::Moon(String::from("I can see the moon")),
	}
}

fn check_skystate(state: &ThingsInTheSky){
    match state{
        ThingsInTheSky::Sun(description) => println!("{description}"),
	ThingsInTheSky::Moon(description) => println!("{description}"),
	}
}

#note #rust