liveBook · Manning

Loop

As explained in Rust in Action: Systems programming concepts and techniques

A for loop repeatedly executes a block while iterating over the elements of a sequence or iterator.

A for loop repeatedly executes a block for each element produced by a sequence, collection, or iterator. Its readable syntax makes iteration convenient while preserving control over ownership and access. section 2.4.1 section 2.15

Choose the loop form according to how the collection should be used:

  • Use `for item in collection` when the loop takes ownership of the collection; afterward, the collection cannot be accessed. section 2.4.1
  • Use `for item in &collection` to iterate through shared references and reuse the collection later. section 2.4.1
  • Use `for item in &mut collection` to modify each element through mutable references. section 2.4.1
  • Use ranges such as `0..10` or `0..=10` with `_` when the loop performs an action a fixed number of times. section 2.4.1
  • Prefer direct iteration over manually maintaining an index; indexing can add runtime cost and introduce indexing problems. section 2.4.1

Shorthand

Equivalent to

Access

for item in collection

for item in IntoIterator::into_iter(collection)

Ownership

for item in &collection

for item in collection.iter()

Read-only

for item in &mut collection

for item in collection.iter_mut()

Read-write

Compare the three common forms of for iteration with their compiler-level equivalents and access modes. section 2.4.1

Iterating through file bytes

A hexdump-style program reads a file into fixed-size buffers with `while let`, then uses `for byte in &buffer` to inspect each byte without consuming the buffer. A second for loop processes each line of buffered input. section 7.3

The compiler expands for syntax into iterator method calls: a collection uses `IntoIterator::into_iter`, a shared reference uses `iter`, and a mutable reference uses `iter_mut`. This high-level construct is designed to carry little runtime overhead. section 2.4.1 section 1.7.3

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains syntax, ownership modes, references, ranges, indexing pitfalls, and compiler expansion, but defers detailed rationale to ownership treatment.
Examples
Uses collection traversal, fixed-count ranges, mutable element updates, and file hexdump processing as concrete examples.
What it assumes
Assumes basic Rust syntax and collections while introducing iteration, ownership effects, references, mutable references, and ranges.
What you can do after
Apply for loops to traverse collections, borrow or mutate elements, repeat fixed actions, and process buffered file data.
How it connects
Connects for loops with iteration, traits, IntoIterator, iterators, collections, ranges, ownership, and borrowing.

Also explained in

Compare how 27 books explain loop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.