Zig Error: unused local variable
error: unused local variable (and its twin, error: unused local constant) means you created a name inside a function and never used it. In most languages that’s a warning you can scroll past. Zig has no warnings at all: anything the compiler complains about stops the build. It’s probably the first Zig error you’ll meet, so let’s make it a quick one. 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 { const name = "Ada"; var count: u32 = 0; std.debug.print("Hello!\n", .{});}main.zig:5:9: error: unused local variable var count: u32 = 0; ^~~~~main.zig:4:11: error: unused local constant const name = "Ada"; ^~~~Two names, two errors. count was made with var, so it’s a “local variable”. name was made with const, so it’s a “local constant”. The rule is the same for both. The caret (^~~~) points at the name, and the numbers after main.zig: are the line and column.
Notice the errors aren’t in line order: line 5 comes before line 4. Zig reports these in the order it finds them, so read every error in the list, not just the top one.
What it means
Section titled “What it means”A local is a name made inside a function or block with const or var. Zig checks that every local is used at least once before the block ends. The check is simple: any mention of the name after it’s declared counts, even assigning to it. So count = 5; on its own is enough to make the error go away, even though nothing ever looks at the value. Don’t rely on that: a value nobody reads is still a bug, the compiler just can’t see it.
Why be so strict? A name you never use is often a sign of a real bug: you computed something and forgot to print it, or you used the wrong name further down. Zig would rather stop you now than let that slip into a program. The 0.9.0 release notes, where this became an error, give the reasons: it helps when you rework code, and it prevents bugs. The Zig Variables lesson covers the rule along with const and var.
How to fix it
Section titled “How to fix it”Fix 1: Use the value
Section titled “Fix 1: Use the value”Most of the time the error is right: you meant to use the name and forgot. Here we meant to greet name and show count:
const std = @import("std");
pub fn main() void { const name = "Ada"; var count: u32 = 0; count += 1; std.debug.print("Hello, {s}! Visit number {d}.\n", .{ name, count });}Output:
Hello, Ada! Visit number 1.Once both names are used in the print call, the errors are gone. If the unused name is something you just computed, like a total, that’s a hint the program forgot to report its result.
Fix 2: Delete it
Section titled “Fix 2: Delete it”If you don’t need the name any more (maybe it’s left over from an older version of the code), delete the line. Less code is the best fix there is:
const std = @import("std");
pub fn main() void { std.debug.print("Hello!\n", .{});}Output:
Hello!Fix 3: Discard it with _ = name; while you work
Section titled “Fix 3: Discard it with _ = name; while you work”Sometimes you’re halfway through a function and want to run it before every name is used. Assigning a value to _ tells Zig “I know, I’m not using this yet”:
const std = @import("std");
pub fn main(init: std.process.Init) void { const gpa = init.gpa; _ = gpa; // TODO: use this for the list later std.debug.print("Not done yet\n", .{});}Output:
Not done yetThis is handy, but treat it as a sticky note, not a fix. Once you start using gpa, you must delete the _ = gpa; line, or you’ll get the next error in this series: pointless discard of local constant.
Fix 4: Use _ in a loop or capture
Section titled “Fix 4: Use _ in a loop or capture”The same rule applies to the names between |...| in a for, while, if or switch. There the error says unused capture:
const std = @import("std");
pub fn main() void { const items = [_]i32{ 4, 5 }; for (items, 0..) |item, i| { std.debug.print("{d}\n", .{item}); }}main.zig:5:29: error: unused capture for (items, 0..) |item, i| { ^We asked for the index i and never read it. Either drop the 0.. and the i (the loop is just for (items) |item|), or write _ in that spot when you need the position but not the value:
const std = @import("std");
pub fn main() void { const items = [_]i32{ 4, 5 }; for (items, 0..) |_, i| { std.debug.print("position {d}\n", .{i}); }}Output:
position 0position 1The Zig For Loops lesson covers loops with an index.
Where you’ll usually see it
Section titled “Where you’ll usually see it”- Half-written code. You declared three names, wrote the code for one, and ran it to check. Fix 3 is made for this.
- After a refactor. You moved some logic into a function and left the old
constbehind. Delete it. const gpa = init.gpa;at the top ofmainin a program that doesn’t allocate yet.- A result you forgot to print, like
const total = add(a, b);with no line that showstotal. - Function parameters have their own version of this: unused function parameter.
Related errors
Section titled “Related errors”- pointless discard of local variable: you kept
_ = x;after you started usingx. - unused function parameter: the same rule for a function’s inputs.
- local variable is never mutated: the
varis used, but never changed. - All Zig error pages: the full list.
Practice
Section titled “Practice”1. The forgotten total
Section titled “1. The forgotten total”This program should print sum: 60, but it doesn’t build. Fix it without deleting anything.
const std = @import("std");
pub fn main() void { const prices = [_]u32{ 10, 20, 30 }; var total: u32 = 0; for (prices) |price| { total += price; } const message = "sum"; std.debug.print("done\n", .{});}Hint: only one name gives an error, but the program has a second problem: it never shows its result. Put both names in the last print.
Show solution
const std = @import("std");
pub fn main() void { const prices = [_]u32{ 10, 20, 30 }; var total: u32 = 0; for (prices) |price| { total += price; } const message = "sum"; std.debug.print("{s}: {d}\n", .{ message, total });}Output:
sum: 60Only message gave an error: total += price mentions total, so the compiler counted it as used. But the program still never showed the total. The compiler catches names you forgot completely, not values you forgot to report, so read your own code with that in mind.
- Zig has no warnings: an unused local is a compile error.
unused local variableis forvar,unused local constantforconst,unused capturefor|x|.- Any mention of the name counts as a use, even
x = 5;. A value nobody reads is still a bug the compiler can’t see. - Fix it by using the name, deleting it, or writing
_ = name;while you’re still working. - Remove
_ = name;once you use the name, or you get pointless discard.
Next up: local variable is never mutated, the error for a var that could have been a const.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Variables in the Zig language reference:
const,varand local variables. - Ignoring values in the Zig language reference: why non-void values must be used and how
_discards them. - The original Zig issue on unused locals: the discussion that made unused locals an error.
- Zig Variables: our lesson on
const,varand the unused-name rule.