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 |
for i in 0..3 { print!("{i} "); } // 0 1 2 let part = &"hello"[1..=3]; // "ell"
Try it
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.
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
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 onlyClone. Writinglet r = 0..3; for i in r.clone()or building a fresh range each time avoids a "moved value" error. - Iterating needs
Step: integers andchariterate; floats do not (0.0..1.0works forcontains, but notfor). RangeInclusiveis bigger and slightly slower to iterate, because it tracks an "exhausted" flag. For hot loops0..nis slightly cheaper than0..=n - 1.- Slicing: all six forms work as index for slices,
Vec,Stringandstr(byte positions on strings). - Match patterns support
a..=banda..and..=b; an exclusive patterna..bis also accepted in modern Rust (range patterns). drain,splice,copy_within,replace_rangeaccept anyRangeBoundsargument.- Unusual bounds like "excluded start" exist only as
(Bound, Bound)tuples. step_by(0)panics, andrev()on aRangeFromis not available because it has no end.
Related
Range operators,[T] slices,Range patterns,for,step_by and rev- Lessons: Loops, Slices, Patterns