Expressions & Flow · 10 min · free lesson

Ranges

Range syntax makes boundaries visible instead of hiding an off-by-one rule.

Core idea

a..b excludes b; a..=b includes b.

Coming from C? From for(;;) to Ranges covers this side by side.

Start with the idea

The range 0..3 contains 0, 1, and 2; its upper bound is excluded. The inclusive form 1..=3 also contains 3. Half-open ranges fit indexing because a length can be used directly as the excluded end. Inclusive ranges are useful when both endpoints belong to the domain.

Read the code

for index in 0..3 {
    println!("index {index}");
}
for value in 1..=3 {
    println!("value {value}");
}

The two ranges have different upper-bound rules.

Practice starter

let values: Vec<_> = (2..5).collect();

Practise this code in Playground ↗

Diagram for Ranges: a Rust code example flows to the lesson rule and the key observation.
The two ranges have different upper-bound rules.

Work through it

Half-open ranges make boundaries compose cleanly. In 0..3, the first value is included and 3 is excluded, so there are exactly three integers. This mirrors a slice whose length is 3: valid positions are 0, 1, and 2, while the length itself is the first position after the slice. Two adjacent spans such as 0..3 and 3..6 meet without overlapping. That is why half-open intervals are common in indexing and iteration.

The inclusive form 1..=3 includes both endpoints and is a natural fit when the domain itself is phrased that way: days one through three, or levels one through three. The syntax is not just decoration. Changing .. to ..= alters which values a for loop sees and may add one more iteration. Write out the sequence before running whenever an off-by-one error would matter.

Ranges are values that can be passed to operations, but the exact kinds they support depend on their element type. A range does not automatically protect every use of its numbers as indexes; you still need to know the collection length. Change the upper bound in the sample to 0..=3 and predict the extra line, then try a zero-length half-open range such as 3..3 and explain why the loop body does not run.

A range communicates which endpoints belong to an operation. 0..n includes zero and excludes n; 0..=n includes both ends. For a collection of length n, valid indices stop at n - 1, so 0..n matches them exactly when n is positive. This is one reason half-open ranges appear often in Rust. The type of a range is not a collection of stored numbers; iteration produces the values as needed.

Write the values generated by 2..5 and 2..=5 before running either loop. Then test an empty range such as 3..3 and explain why its body never runs. For indexing, prefer iterating over items directly when you do not need indices; use enumerate() when you need both an index and an item. Boundary reasoning matters more than memorizing one syntax: state clearly whether the end is inside or outside the operation.

Try it

Check membership of the half-open range start..end, including start but excluding end. Empty ranges contain nothing. In the Crabrix app, open the compiler lab and complete the supplied function in solution.rs. The reading question is a separate exercise.

What success looks like: The local compiler accepts the changed solution and the separate verification harness finishes all normal and boundary cases. Printing a success message alone does not complete this lab.

Check your understanding

Which values are collected?

Reveal the answer
  1. 2, 3, 4
  2. 2, 3, 4, 5
  3. 3, 4, 5

The upper bound of .. is excluded, so 5 is not part of the range.

One more experiment

Change 0..3 to 0..=3 and predict the extra line. Then change the first range to 3..3 and the second to 3..=3, and write how many lines each loop prints before running.

Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Ranges (courseId "basics", lessonId "ranges")
Link: https://crabrix.com/learn/basics/ranges/
Goal: a..b excludes b; a..=b includes b.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/ranges) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.