0Pricing
Zig Academy · Lesson

Iterating and Mutating Slices

Read and write through a slice.

Iterating and Mutating Slices is a free Zig Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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
}

Read-Only by Default

A plain capture gives you a copy of each element. Changing that copy does not touch the slice's underlying data.

Capture by Pointer to Mutate

Add * before the capture name to get a pointer instead of a copy. Now writing through it edits the real element.

for (items) |*value| {
    value.* += 1;
}

Dereference to Write

With a pointer capture you use .* to reach the value it points at. That syntax lets you both read and assign.

value.* = 0;

The Slice Must Be Mutable

To change elements the slice type must be plain []T, not []const T. A const slice forbids writing through it.

Looping with an Index

Pair the slice with a range to also get a counter. The second capture after the comma is the current index.

for (items, 0..) |value, i| {
    // i is the index
}

Indexing Directly

You can also write to a slot by index without a loop capture. Assign through the brackets just like an array.

items[0] = 42;

Iterating Two Slices Together

Pass several slices to one for loop to walk them in lockstep. They must share the same length or it panics.

for (xs, ys) |x, y| {
    // x and y line up
}

Accumulating a Result

Keep a running total in a var declared before the loop, then update it on each pass to compute a sum.

var total: i32 = 0;
for (items) |v| total += v;

Skipping with continue

Inside the loop, continue jumps straight to the next element. Use it to ignore values that fail a quick test.

for (items) |v| {
    if (v < 0) continue;
}

Quick Check

You want to double every element of a []i32 slice in place. Which capture form lets you write back?

Recap

Loop a slice with for: plain captures read copies, |*value| with .* mutates in place, and add a range to track the index. 🎯

Frequently asked questions

Is the “Iterating and Mutating Slices” lesson free?

Yes — the full text of “Iterating and Mutating Slices” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “Iterating and Mutating Slices”?

Read and write through a slice. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Iterating and Mutating Slices” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Zig Academy lesson?

Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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