Skip to content
Justice Tech School

Ranges

a..b excludes b and a..=b includes it. Ranges work in for, slices and match.

Example
fn main() {
    let s: i32 = (1..=5).sum();
    println!("{}", s);
    for i in (0..10).step_by(3) { print!("{} ", i); }
    println!();
    let v = [10, 20, 30, 40];
    println!("{:?}", &v[1..3]);
}

Result

15
0 3 6 9 
[20, 30]

Exercise

Inclusive range.

15

Quiz Test

Does 1..5 include 5?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.