How to match a range?

0100

To match a range in Rust, you can use the ..= syntax, which allows you to specify an inclusive range of values. Here's an example:

let x = 5;

match x {
    1..=5 => println!("one through five"),
    _ => println!("something else"),
}

In this example, if x is any value from 1 to 5 (inclusive), it will print "one through five". If x is outside that range, it will print "something else".

You can also use this syntax with char values:

let x = 'c';

match x {
    'a'..='j' => println!("early ASCII letter"),
    'k'..='z' => println!("late ASCII letter"),
    _ => println!("something else"),
}

Here, if x is between 'a' and 'j', it will print "early ASCII letter". If x is between 'k' and 'z', it will print "late ASCII letter".

0 Comments

no data
Be the first to share your comment!