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.
let half_open = 2..5; // 2, 3, 4 let inclusive = 2..=5; // 2, 3, 4, 5 let tail = &data[3..];
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
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..bwitha >= bis valid and empty. Slicing witha > bpanics.RangeInclusivecan include the maximum value of its type (0..=u8::MAX), which0..256u8could not express...=needs an end:start..=is a syntax error.
Usage notes
- Iteration: only ranges whose bounds implement
Step(integers andchar) are iterators. ARange<f64>hascontainsbut cannot be looped over. - Slicing panics:
&v[2..9]panics when the range is outside the slice. Usev.get(2..9)for anOption. Onstrthe indexes are byte offsets and must lie on character boundaries. - Not
Copy:Rangeis an iterator that mutates itself, so it is deliberately notCopy. Call.clone()or rebuild the range if you need to use it twice. - Precedence:
..binds weaker than+and||, so0..n + 1is0..(n + 1). It is non-associative:a..b..cis an error. See Operator precedence. - Inclusive iterators are slower:
RangeInclusivecarries an extra flag, which can stop some loop optimizations. Prefer0..nwhenncannot overflow. - In patterns:
a..=bis stable everywhere;a..band..=b/a..patterns are also available (exclusive range patterns stabilized in 1.80). Pattern bounds must be literals or constants. - Empty
forbodies: aforover an empty range simply does not run; no special case needed. - Generic code: accept
impl RangeBounds<usize>to take any of the six forms, asVec::drainandString::replace_rangedo.
Related
- for and match
- [] indexing
- Operator precedence
- Tutorial: Slices, Loops, Patterns