Pattern matching in Rust

Ever heard of Rust programming language? Here in this blog, we will look at an important feature of Rust programming language. Have a look in this blog and know why it’s worth learning.

Match is a powerful control flow operator it allows us to compare a value against a series of patterns and then execute code based on which pattern matches, although it seems very similar to the expression used with if but there is a big difference with it. Expression in if needs to return a Boolean value but here in Match it can be of any type, here is an example

enum Num {

fn value_in_integer(num: Num) -> u32 {
    match num {
        Num::One => 1,
        Num::Two => 5,
        Num::Three => 10,
        Num::Four => 25,

When a Match expression executes it compares the resulting value with the pattern of each arm. An arm has two parts: a pattern and some code. The first arm here has a pattern and then the => operator that separates the pattern and the code to run. Each arm is separated from the next with a comma.

Here in the function value_in_integer, it takes an argument of Num type which itself is an enum. Match expression has the variants of enum type as its pattern. When the Match expression executes, it compares the resulting value against the pattern of each arm, in order. If a pattern matches the value, the code associated with that pattern is executed. If that pattern doesn’t match the value, execution continues to the next arm, we can have as many arms as we need.

Rust pattern matching is of an exhaustive type means is you are matching an unsigned integer you need to have at least one case for all the unsigned integer. Here is the example

  let num = 5;

   match num { 

      1 = println!("One"),
      2 = println!("Two"),
      3 = println!("Three"),
      _ = println!("Something more than three"),  


 O/P -> Something more than three

The “_” represents rest of the unsigned integers and this is the reason we are getting the output “Something more than three”.

Another useful feature of Match arms is that they can bind to the parts of the values that match the pattern. As an example, let’s change one of the enum as state which has variant states of INDIA make another enum Lang(Language) changing one of the variant Marathi of the type Maharastra and now you can apply Pattern match on it. This is how we can extract values out of enum variants.

enum State {
    Tamil Nadu,

enum Lang {

We can even achieve polymorphism using match arm like in the following example we implemented the same method for each of the variant of the enum since all variants are of the same type

enum Shape {
 imple shape {
  fn area(&self) -> f64 {
   match *self {
   Shape::Square(ref s) => (s*s) as f64,
   Shape::Circel(ref r) => 3.14*(r*r),

Reference :-


Written by 

Alok Jha is the Trainee Software Consultant at Knoldus Software LLP. He has good knowledge of languages C, C++, Java and currently exploring Rust. As a fresher, he always tries to explore the different type of software and tools.

Leave a Reply

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!