Rust Lang

Rust Match Range

In Rust, the match expression allows us to perform a pattern that matches quickly and efficiently. It enables us to compare a value against a set of patterns and execute a specific block based on the matching value.

We can use the match feature to match a value against a range of values instead of a single value. This is valuable when working with numerical values or any other data type that contains a natural ordering.

In this tutorial, we will explore how we can use the Rust match feature and the range operator to match a specific value against a range of values.

Range Definition

The first step is to define the range against which we wish to match. In Rust, we can define a range using the “..” or the “..=” operators.

The “..” operator creates an exclusive range which includes all values between the start and end values, but excludes the end value itself.

The “..=” operator creates an inclusive range which includes both the start and end values.

For example, to create a range of integers between 1 and 5, we can do the following:

let r = 1..=5;

 
This should create a range of inclusive values from 1 to 5.

Rust Match Range

Once we define the range, we can match an expression against a specific value.

For example, suppose we want to match a given integer against the range of integers from 1 to 10:

let i = 7;
match i_int {
    1..=10 => println!("Value is between 1 and 10"),
    _ => println!("Value is outside the range 1 to 10"),
}

 
In the previous example, the first arm matches the expression against the integer values that are defined in the range from 1 to 10. If the value of the variable “i” falls within that range, we print the corresponding message. If the values fall outside that range, we execute the “_ arm”.

We can also match against multiple ranges using the “|” operator to separate the ranges.

Example:

fn main() {
    let i = 5;

    match i {
        1..=10 | 20..=30 => println!("Value is between the ranges"),
        _ => println!("Value is outside the ranges"),
    }
}

 
In this example, the first arm of the match expression uses the “|” operator to match the value against the range of integers from 1 to 10 or the range of integers from 20 to 30.

We can also use the range operator to match against other data types such as characters and floating point values.

Example:

fn main() {
    let char = 'A';

    match char {
        'A'..='Z' => println!("uppercase"),
        'a'..='z' => println!("lowercase."),
        _ => println!("not a letter"),
    }
}

 
The previous example demonstrates how we can use the ranges to compare the character values with different arms and options.

Conclusion

You now learned how we can work with Rust’s match expression to match a value against a range of values.

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