3 ms·
> Without the let, I'd have to do a match statement followed by an unwrap just to check if a field in an enum is set. You... would not? An `if let` trivially
by masklinn 3y ago
> Without the let, I'd have to do a match statement followed by an unwrap just to check if a field in an enum is set.
You... would not?
An `if let` trivially desugars to a `match`.
if let <pat> = <expr> {
...
}
becomes
match <expr> {
<pat> => {
...
}
_ => {}
}
- trane_project 3y agoSee my example. I am trying to extract the value of an optional field in the enum. That's where the unwrap comes in.
- masklinn 3y agoWhat I wrote up is the spec-defined desugaring of a single-branch `if let`, aside from desugaring bugs there should be no counter-example. The snippet you link to should be replaceable by something along the lines of match &passages.asset { TranscriptionAsset::Track { artist_name: Some(artist_name), .. } => { metadata .entry(ARTIST_METADATA.to_string()) .or_default() .push(artist_name.clone()); } _ => {} } no `unwrap` needed. I don't know where you got that idea.