Rust, Software Development

Rust – Switch Statements Examples Using Match

Rust key value hashmap map convert integer string check NULL value Connect postgreSQL

Rust does not have a switch keyword, but it has the match construct that works like a switch statement in our languages. Generally, the match can run codes or return a single value. It cannot do both at the same time. Let us go through some examples that use the Rust match construct.

Options That Only Run Codes with Match

We start with a simple Rust match statement example. Consider the following codes. We have two functions that only display texts on the console. Using the match construct, we run these functions depending on a numeric value. Okay, we have the user_choice variable that dictates how the match statement works.

The primary function looks as follows.

Line 3 stores the numeric value. We use the user_choice variable with the match keyword to mean that we are passing a value of 10 as an argument to the match construct. Lines 8 – 12, read as follows:

  1. If user_choice is equal to 1, run the f1 function. Otherwise, go to the next expression.
  2. If user_choice is equal to 2, invoke the f2 function. Otherwise, proceed to the next expression and so on.
  3. If user_choice is neither 1 or 2, call the f_unknown function. The last expression in our match statement represents a catch-all condition.

When we run the codes, the function f_unknown() will because there is no matching for the numeric value of 10. For the catch-all condition, we use _ to mean any value, not matching any of the previous expressions. The codes will generate the following output.

Options That Only Return Values from Match

Now, instead of calling functions or running blocks of codes, the match can return values with a little change in the match contract’s syntax. Notice line 4 in the codes – we use the let keyword together with the match keyword. Cool, no?

When we run the codes, we will get the following output.

Mixing Options That Run Codes With Returning Values

Mixing match options that run codes with those that return values will not work. Let us explore it. Consider the following codes.

If we run these codes in this Rust match statement example, we will get the following errors.

Do you believe me now? Okay, let us move on.

Using String and &Str with Match Example

If we are to use string (in lowercase) values in the match statement, we need to use static (or literal) string values. As a result, the argument we pass to the match statement must be of the same type. Consider the following codes.

We start with a value of String type from a literal string “B.”

Then, we convert that value back to a literal string “B” on line 3 to match what string type the match statement expects (at line 4). Cool? Are you still with me? If yes, let us proceed. The codes will generate the following result.

Using Rust enum With Match Example

Now,  for the most exciting part – we can use enums with match statements. Consider the following codes. We have an enum Direction that has NORTH, EAST, WEST, and SOUTH.

When we run the codes, we the following output.

There is one crucial thing we need to keep in mind when using match statements with enum types – we need to list out all the options! Yes! Notice the match statement codes for this section; we list out all the directions. What happens if we forget to include one? Consider the following codes. On line 8, we excluded out the Direction:: WEST.

We get the following error when we run the modified codes.

Those are some match statement examples in Rust.

Further Readings

Here are some links for further readings.

This post is now part of Rust Programming Language For Beginners Tutorial.

Got comments, or suggestions? Please visit our Facebook page!

You Might Also Like