Zig Error: pointless discard of local variable
error: pointless discard of local variable means you have a line like _ = count; that says “I’m not using count”, and then you do use count. The two disagree, so Zig asks you to delete the discard. It’s one of the easiest errors to fix: remove one line. It comes in three flavours, local variable, local constant and function parameter, all with the same fix. 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 count: u32 = 0; _ = count; count += 1; std.debug.print("{d}\n", .{count});}main.zig:5:9: error: pointless discard of local variable _ = count; ^~~~~main.zig:7:32: note: used here std.debug.print("{d}\n", .{count}); ^~~~~The error points at the discard, and the note points at a place where you really use the name. Here that’s the print on line 7, but count += 1 on line 6 counts as a use too: any mention of the name does.
What it means
Section titled “What it means”_ = x; assigns x to _, which throws the value away. It’s the way to silence an unused local variable error while you’re still writing a function. Once you use x, the discard has no job left, and Zig doesn’t let pointless code sit around. Leaving it would also mislead the next reader into thinking x isn’t used. The 0.9.0 release notes, where unused locals became errors, explain where _ = x; comes from.
This error usually comes from a two-step history: you wrote _ = x; (or your editor added it for you), then later wrote the code that uses x and forgot the placeholder.
How to fix it
Section titled “How to fix it”Fix 1: Delete the discard line
Section titled “Fix 1: Delete the discard line”That’s it. Remove _ = count;:
const std = @import("std");
pub fn main() void { var count: u32 = 0; count += 1; std.debug.print("{d}\n", .{count});}Output:
1Fix 2: Same fix for constants and parameters
Section titled “Fix 2: Same fix for constants and parameters”The message changes with the kind of name, but the fix doesn’t. A parameter gives pointless discard of function parameter:
const std = @import("std");
fn show(label: []const u8) void { _ = label; std.debug.print("{s}\n", .{label});}
pub fn main() void { show("hi");}main.zig:4:9: error: pointless discard of function parameter _ = label; ^~~~~main.zig:5:32: note: used here std.debug.print("{s}\n", .{label}); ^~~~~Delete line 4:
const std = @import("std");
fn show(label: []const u8) void { std.debug.print("{s}\n", .{label});}
pub fn main() void { show("hi");}Output:
hiA const gives pointless discard of local constant. Same story.
What about _ = &x;?
Section titled “What about _ = &x;?”You’ll see a different line in our lessons and in the Zig docs: _ = &x;, with an &. That one isn’t flagged as pointless:
const std = @import("std");
pub fn main() void { var count: u32 = 0; _ = &count; count += 1; std.debug.print("{d}\n", .{count});}Output:
1&count takes the address of count, which tells Zig “this might be changed through a pointer”. It’s used to stop local variable is never mutated in small examples and tests, where a value has to stay a runtime variable on purpose. In this program it does nothing useful (the += already changes count), so you’d delete it too. Just know it’s not the same thing as _ = count;.
Where you’ll usually see it
Section titled “Where you’ll usually see it”- After filling in a function you started with placeholder discards.
- After accepting an editor quick fix that added
_ = x;, then usingxa few lines later. - In
main(init)when you wrote_ = init;and later started usinginit.gpaorinit.io. - In callbacks, where you discarded a parameter and later needed it.
Related errors
Section titled “Related errors”- unused local variable: the error the discard was hiding in the first place.
- unused function parameter: for parameters, naming them
_is often better than a discard line. - local variable is never mutated: where
_ = &x;comes from. - All Zig error pages: the full list.
Practice
Section titled “Practice”1. Clean up the placeholders
Section titled “1. Clean up the placeholders”This function was written in two steps and still has its placeholders. Make it build and print Ada: 3 points.
const std = @import("std");
fn report(name: []const u8, points: u32, bonus: u32) void { _ = name; _ = points; _ = bonus; std.debug.print("{s}: {d} points\n", .{ name, points });}
pub fn main() void { report("Ada", 3, 0);}Hint: two discards are pointless, one isn’t. Which parameter is never used?
Show solution
const std = @import("std");
fn report(name: []const u8, points: u32, bonus: u32) void { _ = bonus; std.debug.print("{s}: {d} points\n", .{ name, points });}
pub fn main() void { report("Ada", 3, 0);}Output:
Ada: 3 pointsname and points are used in print, so their discards go. bonus is still unused, so its discard stays (or you could remove the parameter and the 0 in the call).
- The error means a
_ = x;line is left over: you discardxand use it too. - The fix is to delete the discard line.
- It says
local variable,local constantorfunction parameterdepending on the name; the fix is the same. - The
note: used hereshows a place where you use the name. Any mention counts, evenx = 5;. _ = &x;is a different trick and isn’t flagged.
Next up: use of undeclared identifier ‘x’, the error for a name Zig can’t find.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Ignoring values in the Zig language reference: what assigning to
_does. - Variables in the Zig language reference: the rules for local names.
- Compile errors for unused locals (0.9.0 release notes): why unused names are errors, and why discards are meant to be explicit.
- Zig Variables: our lesson on unused names and
_.