Skip to content

Zig Error: local variable is never mutated

error: local variable is never mutated means you declared something with var, but nothing in the function ever changes it. The note under it, consider using 'const', is the fix nine times out of ten. The tenth time, the error has caught a real bug: a value that was supposed to change and doesn’t. This page shows how to tell the two apart. For the other errors in this series, see Zig Errors Explained.

const std = @import("std");
pub fn main() void {
var total: i32 = 10;
std.debug.print("{d}\n", .{total});
}
main.zig:4:9: error: local variable is never mutated
var total: i32 = 10;
^~~~~
main.zig:4:9: note: consider using 'const'

Line 4, column 9 is the name total. “Mutated” just means “changed”: assigned with =, updated with +=, or changed through a pointer.

In Zig, var is a promise: “this value will change”. const says it won’t. The compiler checks the promise. If a var is only ever read, it asks you to say so with const. The Assignment section of the language reference shows the two side by side.

That might feel picky, but it helps whoever reads your code next (often you, next month). When every name that can change is marked var, you can glance at a function and see which values move. The Zig Variables lesson explains const and var from the start.

If you come from JavaScript, it’s the same advice linters give for let versus const. If you come from Rust, it’s like Rust’s “variable does not need to be mutable” warning, except Zig makes it an error.

If the value really never changes, say so:

const std = @import("std");
pub fn main() void {
const total: i32 = 10;
std.debug.print("{d}\n", .{total});
}

Output:

10

With const you can often drop the type too (const total = 10;), because Zig works it out from the value. A var can’t do that with a plain number, which is a different error: variable of type ‘comptime_int’ must be const or comptime.

Before you switch to const, ask: should this value change? Here’s a loop that’s meant to count to 3:

const std = @import("std");
pub fn main() void {
var i: u32 = 0;
while (i < 3) {
std.debug.print("{d}\n", .{i});
}
}
main.zig:4:9: error: local variable is never mutated
var i: u32 = 0;
^
main.zig:4:9: note: consider using 'const'

Following the note here would be wrong: with const i, the program would build and then print 0 forever. The real bug is the missing i += 1. The compiler just caught an infinite loop for you:

const std = @import("std");
pub fn main() void {
var i: u32 = 0;
while (i < 3) {
std.debug.print("{d}\n", .{i});
i += 1;
}
}

Output:

0
1
2

Even better, Zig’s while has a spot for the update, so it can’t get lost: while (i < 3) : (i += 1) { ... }. See Zig While Loops.

You don’t have to assign the whole variable. Changing one field of a struct, or one item of an array, counts as a change:

const std = @import("std");
const Point = struct { x: i32, y: i32 };
pub fn main() void {
var p: Point = .{ .x = 1, .y = 2 };
p.x = 10;
std.debug.print("{d} {d}\n", .{ p.x, p.y });
}

Output:

10 2

Calling a method that takes a pointer (*Point) also counts, which is why var list: std.ArrayList(i32) = .empty; followed by try list.append(gpa, 1); is fine. If you only read fields, use const.

You’ll see this line in our lessons and in the Zig docs:

const std = @import("std");
pub fn main() void {
var index: usize = 2;
_ = &index;
const letters = "abc";
std.debug.print("{c}\n", .{letters[index]});
}

Output:

c

Taking a pointer with &index and throwing it away tells Zig “assume this might change”. We use it to stop the compiler from working everything out while it builds, so an example can show what happens at runtime (like a panic). In your own programs you rarely need it: if a value doesn’t change, const is the honest answer.

  • Habits from other languages, like declaring everything with let or var in JavaScript.
  • Loops that forgot their update, as in Fix 2.
  • After a refactor that removed the only line that changed the variable.
  • Struct values you only read, like var config = loadConfig(); followed by config.port.

This program should print the sum of 1 to 5 (15). It doesn’t build. One var should become const, and one needs an update. Which is which?

const std = @import("std");
pub fn main() void {
var limit: u32 = 5;
var n: u32 = 1;
var sum: u32 = 0;
while (n <= limit) {
sum += n;
}
std.debug.print("{d}\n", .{sum});
}

Hint: limit never needs to change. What about n?

Show solution
const std = @import("std");
pub fn main() void {
const limit: u32 = 5;
var n: u32 = 1;
var sum: u32 = 0;
while (n <= limit) : (n += 1) {
sum += n;
}
std.debug.print("{d}\n", .{sum});
}

Output:

15

limit is only read, so it becomes const. n must change, or the loop never ends; the : (n += 1) part runs after each pass. Making n a const would have built a program that never stops.

  • The error means a var is never changed. Zig wants const for values that don’t change.
  • Usually the fix is to switch to const.
  • But first check if the value should change: a missing update (like i += 1) is a real bug.
  • Changing a field or calling a method that takes a pointer counts as a change.
  • _ = &x; pretends a value might change. It’s for examples and tests, not everyday code.

Next up: variable of type ‘comptime_int’ must be const or comptime, the error for a var without a type.

Quick check

1. What does "mutated" mean in this error?
2. A while (count < 10) loop gives this error for count. What should you check first?
3. var p: Point = .{ .x = 1, .y = 2 }; is followed by p.y = 5;. Do you get this error?