Although it may seem trivial, a switch case is a fundamental decision-making construct in programming. In this tutorial, we will learn how to use the switch statement in Rust.
Rust Match Keyword
In Rust, we do not have a switch keyword. Instead, Rust provides us with the keyword match that allows us to specify matching cases and the corresponding actions.
We start with the keyword match, followed by the variable we wish to match. Then, we open the match body using a pair of curly braces. The match body holds all the test cases against which we wish to compare.
The syntax is expressed as shown below:
match variable {
value => // do
value1 => //do
value2 => // do
value3 => // do
- => //do
}
Let us explore how to apply the match construct in a program.
Match Single Value
Rust uses pattern matching to check if the value specified is equal to the value of the variable. Take the example program below:
let num = 10;
match num {
10 => println!("Ten"),
_ => println!("Other")
}
}
If you are coming from other programming languages, such as the C programming language, you may notice that Rust does not have a default case. This is because only the matching case is executed. All other non-matching cases are handled using the _ case.
Running the previous code should print:
Match Multiple Cases
To match multiple cases, you can add them one after the other, separating them by a comma as shown:
let num = 10;
match num {
10 => println!("Ten"),
11 => println!("Eleven"),
12 => println!("Twelve"),
13 => println!("Thirteen"),
14 => println!("Fourteen"),
_ => println!("Other")
}
}
The previous example should match the value of the variable with each of the specified case. Note: Is the previous method repetitive? We can solve this by using the pipe operator to match multiple values in a single case.
Take the following example code:
let num = 10;
match num {
13|14|15|16|17|18|19 => println!("Teens!"),
_ => println!("Other")
}
}
In the previous example, we specify a case with multiple values using the pipe operator. The last code should return as shown below:
Range Match
We can also match a range of values. Take the following example:
let num = 10;
match num {
0..=17 => println!("Under age"),
18..=25 => println!("Tickets are 10$"),
26..=35 => println!("Tickets are 15$"),
36.. => println!("Above age"),
_ => println!("Invalid age value")
}
}
In the previous example, we use the range operator (inclusive) to match the case within a range of values.
The previous code should return an output as shown below:
Match String
The match keyword is not reserved for integer types. You can compare string values as well. An example code is provided below:
let db = "MySQL";
match db {
"MySQL" => println!("Port 3306"),
"PostgreSQL" => println!("port 5432"),
"MongoDB" => println!("Port 27017"),
_ => println!("Unsupported DB")
}
}
The previous code should return as shown below:
Conclusion
This article discussed how to create and use switch statements in Rust using the match keyword. Also, we discussed the switch case and its importance as an essential decision-making construct. We hope you found this article helpful. Check the other Linux Hint article for more tips and information.