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.
The error message
Section titled “The error message”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.
What it means
Section titled “What it means”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.
How to fix it
Section titled “How to fix it”Fix 1: Change var to const
Section titled “Fix 1: Change var to const”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:
10With 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.
Fix 2: Add the update you forgot
Section titled “Fix 2: Add the update you forgot”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:
012Even 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.
Fix 3: Changing a field counts
Section titled “Fix 3: Changing a field counts”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 2Calling 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.
Fix 4: _ = &x; in examples and tests
Section titled “Fix 4: _ = &x; in examples and tests”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:
cTaking 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.
Where you’ll usually see it
Section titled “Where you’ll usually see it”- Habits from other languages, like declaring everything with
letorvarin 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 byconfig.port.
Related errors
Section titled “Related errors”- cannot assign to constant: the opposite mistake, changing a
const. - variable of type ‘comptime_int’ must be const or comptime: a
varwith no type. - unused local variable: the variable isn’t used at all.
- All Zig error pages: the full list.
Practice
Section titled “Practice”1. Find the real bug
Section titled “1. Find the real bug”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:
15limit 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
varis never changed. Zig wantsconstfor 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
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Variables in the Zig language reference:
const,varand when each is allowed. - Local variables in the Zig language reference: rules for variables inside functions.
- while in the Zig language reference: the continue expression
: (i += 1). - Zig Variables: our lesson on
constandvar.