Enums: if comparison instead of match


#1

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?


#2

Yep! https://doc.rust-lang.org/book/if-let.html

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

#3

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

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

#4

Thanks!