Silly idea: range constructors

⚓ Rust    📅 2025-12-19    👤 surdeus    đŸ‘ī¸ 1      

surdeus

Just 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?


  1. is there any 'fun' category here? â†Šī¸Ž

  2. like struct and variant names â†Šī¸Ž

2 posts - 2 participants

Read full topic

đŸˇī¸ Rust_feed