Enhance code quality using `matches` macro! in Rust

Reading Time: 3 minutes

Code quality is one of the most important aspects of the programming world. It impacts our overall software quality and impacts how safe, secure, and reliable our codebase is.
Code quality is not only to perform efficiently but also to make code more readable.

In this article, we’ll try to get details of the matches macro, and we will also see the scenarios where we can incorporate this macro to improve our code quality.

matches macro!

matches(macro) is provided by Rust’s standard library and an external crate called matches.
Here we’ll talk about the matches macro provided by our standard library only, although both matches provide the same functionality but let’s focus on the official one.

So matches checks whether the given expression matches any of the given patterns or not.
The return type of this macro is a boolean. It returns true if the provided pattern matches, false otherwise.
This macro makes our code cleaner and easy to read. In shot it improves code readability and quality.

Syntax

matches! (expression, pattern)

Let’s understand this syntax:
Here we have to provide two parameters, the expression, and pattern.
expression: a statement that evaluates to some value or some value,
pattern: any condition for which we want to check our expression

For example:

pub enum Test {
    FIRST,
    SECOND
}

fn is_first(data: Test) -> bool {
    matches!(data, Test::FIRST)
    
}

fn main() {
println!("{}", is_first(Test::FIRST))
}

Okay! now we get the idea in terms of the role of this macro and how to use it. Now let’s understand some internal behavior of this like, how it deals with things internally.

Internals of matches macro

macro_rules! matches {
    ($expression:expr, $( $pattern:pat )|+ $( if $guard: expr )? $(,)?) => {
        match $expression {
            $( $pattern )|+ $( if $guard )? => true,
            _ => false
        }
    }
}

This is the internal implementation of this Marco.
It checks whether the expression matches the pattern using the if statement and the match expression.
In the match expression, it checks whether the provided details satisfy the pattern or not. If the expression satisfies the pattern then it will return true, else false from the default section.

How matches helps to improve our code readability and quality

Let’s take the same scenario that we have used above, to check the enum value.

Let’s handle this with match expression
fn is_first(data: Test) -> bool {
    match data {
            Test::FIRST => true,
            _ => false,
        }  
    
}
Now, with matches macro
fn is_first(data: Test) -> bool {
    matches!(data, Test::FIRST)
}

Hope you get the difference between both approaches.
matches turn four lines of code into one line, and now it is easier to read also.
This is how we can improve our code readability or quality by incorporating this Marco.

That’s all for this article thank you for reading.

If you want to read more content like this?  Subscribe Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe Rust Times Newsletter: https://bit.ly/2Vdlld7 .

This image has an empty alt attribute; its file name is screenshot-from-2020-06-08-11-00-35.png

Knoldus-blog-footer-image

Written by 

Pawan Singh Bisht is a Software Consultant at Knoldus Software LLP, having a strong experience of more than two years in the technology field. He has been well versed in the core implementation of Rust and Java. He loves to contribute to the community which he attained from the community.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading