Enums: if comparison instead of match

Hello folks,
my question is:

If I have an enum:

pub enum SList<T> {
    ListElement{e : T, next : Box<SList<T>>},
    Null
}

I want to check if the SList Element has a specific type from the enum, somtehing like this:

let list : SList<T>;
if SList::ListElement?(list) {
    do this
} else {
    do that
}

Is this possible without using "match" and ask all types?

Yep! if let

if let Slist { e, next } = list {
   do this
} else {
   do that
}
1 Like

There's also the equivalent to what @matklad wrote in match form:

match list {
    SList::ListElement{e, next} => do this,
    _ => do that,
}
1 Like

Thanks!