Pattern matching
One of my favourite features of Rust is pattern matching. It’s a simple and elegant way to deal with not just structs, but also enums!
enum ItemKind {
Struct(String, Vec<Field>),
Function(String, Body),
}
impl ItemKind {
fn name(&self) -> &str {
match self {
Self::Struct(name, _) => name,
Self::Function(name, _) => name,
}
}
}
Here, we have an enum and a function to get the name out of this. In C, this would be very unsafe, as we cannot be guaranteed that our union has the right tag. But in Rust, the compiler nicely checks it all for us. It’s safe and expressive (just like many other features of Rust).

Comments
Nothing yet. Say the first thing.
Sign in to join the conversation.