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
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


























