Closed
Description
When I was doing rustlings, I found out it is possible to match with a range which is coded like this:
fn color(n: i32) -> Option<u8> {
match n {
0..=255 => Some(n as u8),
_ => None,
}
}
but when I trying to replace the range with a const,
use std::ops::RangeInclusive;
const RGB_RANGE: RangeInclusive<i32> = 0..=255;
fn color(n: i32) -> Option<u8> {
match n {
RGB_RANGE => Some(n as u8),
_ => None,
}
}
the compiler gives error:
error[E0308]: mismatched types
--> src/main.rs:7:9
|
3 | const RGB: RangeInclusive<i32> = 0..=255;
| ----------------------------------------- constant defined here
...
6 | match n {
| - this expression has type `i32`
7 | RGB => Some(n as u8),
| ^^^
| |
| expected `i32`, found struct `std::ops::RangeInclusive`
| `RGB` is interpreted as a constant, not a new binding
| help: introduce a new binding instead: `other_rgb`
|
= note: expected type `i32`
found struct `std::ops::RangeInclusive<i32>`
I have no idea why matching a literal range is possible but not matching a constant which type is RangeInclusive.