Programming Languages
Rust
Subjective
Oct 04, 2025
How does pattern matching work in Rust with match expressions?
Detailed Explanation
Pattern matching in Rust:
• Exhaustive matching required
• Destructures data types
• Guards with if conditions
• Wildcard _ for catch-all
• Must handle all cases
Example:
enum Coin {
Penny, Nickel, Dime, Quarter
}
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter => 25,
}
}
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts