0Pricing
Zig Academy · Lesson

Iterating and Mutating Slices

Read and write through a slice.

Walking a Slice

Once you hold a slice, you usually want to visit every element. Zig's for loop iterates a slice cleanly from start to end. 🔁

The for Over Items

Put the slice in parentheses and name each element with a capture in pipes. The loop hands you one value per pass.

for (items) |value| {
    // use value
}

All lessons in this course

  1. Fixed-Size Arrays
  2. Slices: A Pointer Plus a Length
  3. Slicing with array[start..end]
  4. Iterating and Mutating Slices
← Back to Zig Academy