Rust Lang

How to Use Match Statements in Rust

A classic construct is supported by most programming languages, otherwise known as the switch statement. Although it takes a few syntaxes and different formatting, it allows us to match a specific case or condition and take the required action in various languages.

In Rust, the match keyword implements the syntax for the switch case. Let us explore how to use the match statement in Rust.

Match Statement in Rust

We will start with the keyword match, and then compare the variable to use the match construct. We then open the match body, which takes the case as a “matched” value against the specified variable’s value.

The syntax is as shown below:

match variable {
  value => // do this
  value2 => // do this
}

Consider the example below that matches a single case:

fn main() {
  let age = 18;
  match age {
    18 => println!("Hi!"),
    _ => println!("Unreachable")
  }
}

In the previous example, we start by initializing the variable age. Then, we use the match statement to check if the age is equal to 18; if true, we print “Hi”. If the variable’s value does not match 18, we print “Unreachable”.

The underscore, in this case, shows the handle of other undefined cases.

The previous code should return as follows:

To match multiple values, you can use the pipe operator, as shown in the example below:

fn main() {
  let age = 18;
  match age {
    18 | 19 | 20 | 21 | 22 => println!("Hi!"),
    _ => println!("Unreachable")
  }
}

This should check if the value of the variable is equal to 18,19,20, 21, or 22. If true, it executes the specified block.

To match a range of values, you can use the range operator as shown below:

fn main() {
  let age = 18;
  match age {
    18..=22 => println!("Hi!"),
    _ => println!("Unreachable")
  }
}

The previous example should match the values between 18 to 22, inclusive.

Keep in mind that the match operator is not limited to integer types. You can match for string values or Boolean types as shown in the example below:

fn main() {
  let boolean = true;
  let active = match boolean {
    false => 0,
    true => 1,
  };
  println!("Active: {}", active)
}

The previous example uses a Boolean type to check for matching cases. Note that we assign a value instead of printing a value to the console.

To match a string, we can do the following:

fn main() {
  let string = "hi";
  match string {
    "hi" => println!("Hi Back!"),
    "hello" => println!("No Hi Back!"),
    _ => println!("Unknown greeting!")
  };
}

The previous code should print:

Conclusion

This tutorial covers the match statement in Rust and how we can use it to compare different cases. In addition, we discussed how the match keyword implements the syntax for the switch case and provided examples. We hope you found this article helpful. Check the other Linux Hint articles for more tips and information.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list