How can I test if a value lies within a Range?
You can use match
too:
fn main() {
let v = 132;
match v {
1...100 => println!("Inside"),
_ => println!("Outside")
}
}
There is nothing on Range
itself (at present, anyway), but it is not difficult to do this; after all, it just takes a couple of comparisons:
4 >= a.start && 4 < a.end
As of Rust 1.35, the original code will almost1 compile as-is using Range::contains
:
fn main() {
let a = 3..5;
assert!(a.contains(&4));
}
1 Passing &4
instead of 4
to contains()
.