Rust, Software Development

Rust (Switch) Using Match With Examples

Rust does not have a switch keyword but the match keyword that works like a switch examples in other languages like Java and C++. 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 (aka Switch)

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 pass 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 following expression.
  2. If user_choice is equal to 2, invoke the f2 function. Otherwise, proceed to the following expression and so on.
  3. If user_choice is neither 1 nor 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 previous expressions. The codes will generate the following output.

Options That Only Return Values from Match (aka Switch)

Instead of calling functions or running blocks of codes, the match can return values with a bit of 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.

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

There’s no switch in Rust. Okay, let us move on.

Using String and &Str with Match Example

If we 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 String type value 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.

What if we have a String type value and want to match it against a set of &str values?

Consider the following Rust code example.

In this example, we use the as_str method to convert the String value to a slice of &str. We can then match on the slice using the patterns “hello”, “world”, and _, which represent any other value.

You can also use the match keyword to perform pattern matching on string slices, which are references to substrings of String or &str values. For example:

In this example, we are using the &s[0..5] expression to create a slice of the first 5 characters of the &str value s, and then matching on that slice.

Switching 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 with NORTH, EAST, WEST, and SOUTH.

When we run the codes, we get 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 all directions. What happens if we forget to include one? Consider the following codes. On line 8, we excluded the Direction:: WEST.

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

Those are some match statement examples in Rust.

Further Readings Rust Match (aka Switch)

Here are some links for other readings.

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

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like