Skip to content

for loops

Level: 101 · for newcomers

One line: for x in 1..5 walks a range and for elem in [2, 4, 8] walks a collection — and both are the same mechanism, because for does not know about ranges or arrays at all, only about iterators.

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

  • 1..5 is exclusive: four iterations, ending at 4. 1..=5 is the inclusive form, and the = is the whole difference
  • A range is a value — let r = 1..5; — not syntax belonging to for
  • Iterating an array, a Vec, a slice; and the three ways to ask (iter, iter_mut, into_iter), which decide whether the loop borrows or consumes
  • for x in v moves v. The commonest beginner error in this section is using the collection afterwards, and the fix is &v
  • .enumerate() when the index is wanted, rather than a hand-managed counter
  • What the loop desugars into, briefly — enough to make "for is iterator sugar" concrete rather than a slogan
  • Why the idiomatic Rust for many for loops is not a for loop at all: map, filter, sum

The trap it exists for

for x in v consumes v, and the error arrives on a later line — E0382, borrow of moved value — pointing at the innocent use rather than at the loop that took it. One & fixes it, and knowing which & is the whole of iter, iter_mut, into_iter.

See also

Po polsku

for nie wie nic o zakresach ani o tablicach — zna wyłącznie iteratory, dlatego for x in 1..5 i for elem in [2, 4, 8] to ten sam mechanizm, a sam zakres jest zwyczajną wartością, którą można przypisać (let r = 1..5;). Po polsku najłatwiej zapamiętać 1..5 jako przedział prawostronnie otwarty: cztery obiegi, ostatni dla 4 — wersję domkniętą pisze się 1..=5 i cała różnica siedzi w tym jednym znaku =. Pułapka jest gdzie indziej: for x in v przenosi własność wektora (pętla bierze into_iter), a błąd E0382, „borrow of moved value”, pojawi się dopiero w którejś z późniejszych linii i wskaże niewinne użycie zamiast pętli, która wektor zabrała — lekarstwem jest jeden znak, for x in &v, bo wybór między iter, iter_mut a into_iter to właśnie pytanie, czy pętla pożycza, czy konsumuje. Indeks bierze się z .enumerate(), a nie z ręcznie prowadzonego licznika.

Szukaj po polsku: pętla for w Ruscie · przedział prawostronnie otwarty · przeniesienie własności w pętli · rust for loop moves vector E0382 · rust range inclusive 1..=5