Getting Started with Rust (Part 4): Pattern Matching, Error Handling, and Concurrency Basics
Rust is a systems programming language that focuses on safety, speed, and concurrency.
In this article, we will cover the basics of Pattern Matching, Error Handling, and Concurrency.
Remember you can test RUST online here. This article is a continuation of the series of articles here.
Pattern Matching
Pattern matching in Rust is a powerful feature that allows you to compare a value against a series of patterns and execute code based on which pattern matches.
The main construct for pattern matching is the match
statement.
Let’s see some examples!
Example Code: Pattern Matching
fn main() {
let number = 7;
match number {
1 => println!("One"),
2 | 3 | 5 | 7 | 11 => println!("This is a prime number"),
13..=19 => println!("A teen number"),
_ => println!("A different number"),
}
}
To give you a feeling how it works, let’s break it down:
let number = 14;
: This declares and initializes the variablenumber
with the value14
. You can change this number to any number you want to.match number {
…