Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Primitive Types

Rust range types

Reference for Rust range types: a..b, a..=b, a.., ..b, ..=b and .., their iterator behavior, contains, slicing, RangeBounds, and common pitfalls.

Range expressions build small values that describe an interval. Each syntax form has its own type in std::ops, and the ones with a start also work as iterators over integers and chars. You meet ranges in for loops, slicing (&v[1..3]), contains checks, and any API that accepts "a span of positions".

Syntax Type Meaning
a..b Range<T> a up to but not including b
a..=b RangeInclusive<T> a through b
a.. RangeFrom<T> a onwards, no end
..b RangeTo<T> everything before b
..=b RangeToInclusive<T> everything up to and including b
.. RangeFull everything
Rust
for i in 0..3 { print!("{i} "); }   // 0 1 2
let part = &"hello"[1..=3];          // "ell"

Try it

Rust Iterating and testing membership

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 Slicing and RangeBounds

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
let r: Range<i32> = 1..4;
let r = RangeInclusive::new(1, 4);
r.start; r.end                // fields of Range
r.start(); r.end()            // methods of RangeInclusive
r.contains(&x)
Item Description
contains(&x) True if x lies in the range; works for any PartialOrd type, including floats.
is_empty() True when nothing would be yielded or contained (start >= end, or start > end for inclusive).
len() Available via ExactSizeIterator for ranges of small integer types.
RangeBounds<T> Trait implemented by every range type and by (Bound<T>, Bound<T>); use it to accept any range as an argument.

Pitfalls: an upside-down range such as 5..2 is empty, not reversed; use (2..5).rev(). Slicing past the end panics. start..=end with end == MAX is fine for inclusive ranges but start..end cannot reach MAX.

Usage notes

  • Ranges are iterators, not Copy. Range<T> holds mutable iteration state, so it is only Clone. Writing let r = 0..3; for i in r.clone() or building a fresh range each time avoids a "moved value" error.
  • Iterating needs Step: integers and char iterate; floats do not (0.0..1.0 works for contains, but not for).
  • RangeInclusive is bigger and slightly slower to iterate, because it tracks an "exhausted" flag. For hot loops 0..n is slightly cheaper than 0..=n - 1.
  • Slicing: all six forms work as index for slices, Vec, String and str (byte positions on strings).
  • Match patterns support a..=b and a.. and ..=b; an exclusive pattern a..b is also accepted in modern Rust (range patterns).
  • drain, splice, copy_within, replace_range accept any RangeBounds argument.
  • Unusual bounds like "excluded start" exist only as (Bound, Bound) tuples.
  • step_by(0) panics, and rev() on a RangeFrom is not available because it has no end.