Ranges¶
Level: 201 · working knowledge
One line: 0..5 is not syntax for a loop — it is a value of type Range<i32>, a struct with start and end that is also an iterator — which is why it can be stored, passed, sliced with, matched against, and why iterating it consumes it.
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The bullets below are the questions the finished page has to answer.
What it has to cover¶
- The six range types behind
a..b,a..=b,a..,..b,..=band.., and which of them can be iterated (a range with no start cannot) - Half-open by default:
0..lennever needs a- 1, and..=for the case that includes its end - Ranges beyond
for: slicing (&v[1..3]),contains,rev,step_by,lenon an exact-size range,(1..=n).sum() - Ranges in patterns:
0..=9 =>in amatch(see one arm, many values) Rangeis anIteratorand therefore notCopy: storing one in a struct and iterating it twice — and the E0382 "use of moved value" that follows- An empty or reversed range (
5..0) iterates nothing and does not panic; slicing with one does panic - RFC 3550's new range types: which parts, if any, are stable on 1.98.0 — check before writing a word about it
The trap it exists for¶
Writing for i in 0..=v.len() to "include the last element". The last valid index is len - 1, so the inclusive range reads one past the end and panics. A plain 0..v.len() — or iterating &v directly — was right.
Where this sits¶
for loops uses ranges as the thing a loop walks. This page is the type itself. String slices and arrays and slices use ranges as indices.
See also¶
forloops — the loop that walks a range- One arm, many values —
0..=7as a pattern - Arrays and slices —
&five[1..3], half-open - Iterators are lazy — a range adapter computes nothing until consumed
DoubleEndedIteratorandExactSizeIterator— why a range hasrevandlenzipandenumerate— the usual replacement for0..lenindexing
If you are coming from another language¶
- Python.
range(0, 5)is the same half-open sequence and is also a lazy object;range(0, 6)is how Python spells0..=5. Python's range is reusable, while Rust's is consumed by iteration. - Go.
for i := range 5(Go 1.22) andfor i := 0; i < n; i++cover the loop use; Go has no range value you can store or pass. - Java.
IntStream.range(0, 5)andrangeClosedmap directly, including being single-use like Rust's. - C. A
forloop's three clauses; there is no range value, so slicing needs a pointer and a length instead of&v[a..b].
Po polsku¶
Zakres 0..5 to nie składnia pętli, tylko wartość typu Range<i32> — struktura z polami start i end, która jest też iteratorem. Dlatego zakres można przechować, przekazać, użyć do wycinania i w dopasowaniu wzorca; i dlatego iteracja go zużywa — Range nie jest Copy.
Szukaj po polsku: zakres w Ruście · przedział półotwarty · rust range inclusive · rust range not copy