Silly idea: range constructors
â Rust đ 2025-12-19 đ¤ surdeus đī¸ 1Just a silly[1] idea that came up my head:
What if we changed the range operators (.. and friends) into proper constructors[2] so they can be used in patterns like this:
fn fun(some_range: Range<i32>) -> &'static str {
match some_range {
1.._ => "starts in 1",
_..10 => "ends in 10",
(..43)..(43..) => "contains the answer", // yeah, the +1 here is kinda ugly ...
(..7)..(4..) => "overlaps with 4..7",
// the following one may be ambiguous, but ranges are not literals and they are not comparable, so it should be fine
(11..13)..(15..17) => "a funny one",
_ => "something weird",
}
}
What do you think?
2 posts - 2 participants
đˇī¸ Rust_feed