Let's say you have an enum
enum Expr {
binExp, unExp, Literal, Group
}
And you want to call methods on Expr.
Would you rather :
match Expr {
Expr::binExp => todo!(),
Expr::unExp => todo!(),
Expr::Literal => todo!(),
Expr::Group => todo!()
}
OR
if let Expression::binExp = self {
todo!()
} else if let Expression::unExp = self {
todo!()
} else if let Expression::Lit = self {
todo!()
} else if let Expression::Group(e) = self {
todo!()
} else {
todo!()
}
Stated another way, is match a parallel operation? I know if/else will go through each expression sequentially, evaluating them as they are encountered as false and finally make a stop.
Do match statements mimic this behavior or do they directly jump to the correct pattern? I ask this specifically because match can match on multiple patterns, but always select the pattern that matches it first. So it does sound like match may be sequential in that regard.
matchandif letchains deploy powerful optimizations to generate the fastest possible code. Nothing is guaranteed, but for the simple cases outlined in the question, I would expect the same (optimal) code to be generated by both.