Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Operators and Expressions

Rust range expressions: .. and ..=

Reference for Rust range expressions: a..b, a..=b, a.., ..b, ..=b and .., the std::ops range types, iteration, slicing, patterns and bounds.

The operators .. and ..= build range values. a..b is half-open (it includes a but not b), a..=b is inclusive of both ends, and either end can be left out. Each form is a different struct in std::ops: Range, RangeInclusive, RangeFrom, RangeTo, RangeToInclusive and RangeFull.

Ranges are used for three main things: looping (for i in 0..n), slicing (&v[1..3]) and matching (1..=9 => ...). Ranges of integers and char are iterators, so all the iterator methods work on them. Ranges themselves are ordinary values you can store, pass and return.

Rust
let half_open = 2..5;      // 2, 3, 4
let inclusive = 2..=5;     // 2, 3, 4, 5
let tail = &data[3..];

Try it

Rust The six forms

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Ranges in patterns and as values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
start..end          // Range<Idx>            start <= x < end
start..             // RangeFrom<Idx>        start <= x
..end               // RangeTo<Idx>          x < end
..                  // RangeFull
start..=end         // RangeInclusive<Idx>   start <= x <= end
..=end              // RangeToInclusive<Idx> x <= end
Name Description
start, end Values of the same type Idx (integers, char, or anything PartialOrd for containment checks).

Useful methods: contains(&x), is_empty(), start/end fields (for Range, RangeFrom, RangeTo), start()/end() methods (for RangeInclusive), len() on integer ranges, rev(), step_by(n), and everything from Iterator.

Edge cases:

  • a..b with a >= b is valid and empty. Slicing with a > b panics.
  • RangeInclusive can include the maximum value of its type (0..=u8::MAX), which 0..256u8 could not express.
  • ..= needs an end: start..= is a syntax error.

Usage notes

  • Iteration: only ranges whose bounds implement Step (integers and char) are iterators. A Range<f64> has contains but cannot be looped over.
  • Slicing panics: &v[2..9] panics when the range is outside the slice. Use v.get(2..9) for an Option. On str the indexes are byte offsets and must lie on character boundaries.
  • Not Copy: Range is an iterator that mutates itself, so it is deliberately not Copy. Call .clone() or rebuild the range if you need to use it twice.
  • Precedence: .. binds weaker than + and ||, so 0..n + 1 is 0..(n + 1). It is non-associative: a..b..c is an error. See Operator precedence.
  • Inclusive iterators are slower: RangeInclusive carries an extra flag, which can stop some loop optimizations. Prefer 0..n when n cannot overflow.
  • In patterns: a..=b is stable everywhere; a..b and ..=b/a.. patterns are also available (exclusive range patterns stabilized in 1.80). Pattern bounds must be literals or constants.
  • Empty for bodies: a for over an empty range simply does not run; no special case needed.
  • Generic code: accept impl RangeBounds<usize> to take any of the six forms, as Vec::drain and String::replace_range do.