0Pricing
Zig Academy · Lesson

var for Mutable State

When and how to allow reassignment.

var for Mutable State is a free Zig Academy lesson on CoddyKit — lesson 2 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.

When Values Change

Some values truly need to move: a counter, a running total, a current score. For those, Zig gives you the var keyword.

Meet var

The keyword var creates a mutable binding. Unlike const, the name you declare with var can be reassigned again and again.

var score = 0;

Reassigning Freely

Once a value is a var, updating it is just a plain assignment. No special syntax is needed, only the name and a new value.

var score = 0;
score = 10;

Updating in Place

You can compute a new value from the old one. Adding to a counter is the classic example of useful mutation in a loop.

var count: u32 = 0;
count += 1;

Type Often Needed

For a var that starts at zero, Zig may ask for a type so it knows the integer width. Annotate it once and you are set.

var total: i64 = 0;

Unused Mutation Warns

If you declare a var but never actually change it, Zig warns you. It nudges you to use const instead for a clearer intent.

var Inside Functions

Mutable locals live inside functions and last for that scope. A loop accumulator declared with var is a perfect everyday use.

var sum: u32 = 0;
sum += value;

The Cost of Mutation

Every var is one more thing that can change underfoot. Use it where change is the point, not as a lazy default choice.

Loop Counters

A while loop often drives a var forward each pass. The binding updates, the condition rechecks, and the loop advances.

var i: usize = 0;
while (i < 5) : (i += 1) {}

Same Type, New Value

A var can change its value but never its type. An i32 stays an i32; you cannot suddenly store text in a number binding.

var vs const Recap

Think of it simply: const is a value pinned in place, while var is a value that is meant to move. Reach for var only when it must.

Quick Check

You need a counter that increases inside a loop. Which keyword should declare it?

Recap

You learned that var creates a mutable binding for values that change. Annotate its type when needed, and use it only where mutation is truly the goal. 🔁

Frequently asked questions

Is the “var for Mutable State” lesson free?

Yes — the full text of “var for Mutable State” 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 “var for Mutable State”?

When and how to allow reassignment. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “var for Mutable State” 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. const for Values That Never Change
  2. var for Mutable State
  3. Type Inference vs Explicit Types
  4. undefined and Uninitialized Memory
← Back to Zig Academy