Range inconsistencies

I'm trying to wrap my head around ranges, it seems they're a bit inconsistent in cases.

For example in a for loop it's lowerInclusive..upperExclusive or lowerInclusive..=upperInclusive.

In a match it's lowerInclusive...upperInclusive.

The three dot's don't work in for loops and the two dots don't work in matches. What's the difference?

fn main() {
    for i in 1..5 {
        println!("Got {}", i);
    }
    println!("--------------------------");
    for i in 1..=5 {
        println!("Got {}", i);
    }
    println!("--------------------------");
    // Error
    // for i in 1...5 {
    //     println!("Got {}", i);
    // }
    // println!("--------------------------");
    for i in [0, 1, 5, 6].iter() {
        print!("For {} it's ", i);
        match i {
            1...5 => println!("1 to 5"),
            // Error
            // 6..7 => println!("6"),
            _ => println!("not 1 to 5"),
        }
    }
}

..= works with patterns, ... is deprecated and will be linted against in Rust 1.37.

6 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.