liveBook · Manning

Interval

As explained in Learn Rust in a Month of Lunches

An interval is a connected set or span of values between specified lower and upper endpoints, with endpoint inclusion determined by its definition.

A Rust range marks a span from a starting value to an endpoint. The `..` form excludes its endpoint; `..=` includes it. For example, `2..5` contains 2, 3, and 4. section 3.1.1 section 3.2.3

fn main() {
    let array_of_ten = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
 
    let two_to_five = &array_of_ten[2..5];     #1
    let start_at_one = &array_of_ten[1..];     #2
    let end_at_five = &array_of_ten[..5];      #3
    let everything = &array_of_ten[..];        #4
 
    println!("Two to five: {two_to_five:?},
Start at one: {start_at_one:?},
End at five: {end_at_five:?},
Everything: {everything:?}");
}

These ranges select parts of an array: `2..5` selects indexes 2, 3, and 4, while omitting a boundary selects from the start or through the end. section 3.1.1

Ranges also provide values for `for` loops. Use `0..3` to loop over 0, 1, and 2, or `0..=3` to include 3. section 3.2.3

How Learn Rust in a Month of Lunches covers it

How deep it goes
Explains exclusive and inclusive endpoints through array-slicing and loop examples.
Examples
Shows `2..5` slicing a ten-element array and compares `0..3` with `0..=3` in loops.
What it assumes
Explains range-based array slicing using zero-based indexes.
What you can do after
Apply Rust range syntax to select array slices or iterate over a chosen sequence of values.
How it connects
Connects ranges with array slices, zero-based indexing, and `for` loops.

Also explained in

Compare how 7 books explain interval →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.