Zig Error: use of undeclared identifier 'x'
error: use of undeclared identifier 'x' means Zig can’t find anything called x from the spot where you used it. An identifier is just a name: a variable, a constant, a function, a type. Four mistakes cause nearly all of these: a typo, a missing @import, a name used outside the block it was made in, and a struct field written without self.. 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 message = "hi"; std.debug.print("{s}\n", .{mesage});}main.zig:5:32: error: use of undeclared identifier 'mesage' std.debug.print("{s}\n", .{mesage}); ^~~~~~The name in quotes is exactly what you typed, and the caret shows where. Zig doesn’t suggest a correction, so compare the quoted name with the names you meant, letter by letter. Here, mesage is missing an s.
What it means
Section titled “What it means”Every name in Zig has to be declared before Zig can use it: with const or var, as a function parameter, as a loop capture like |item|, or as a top-level fn or const in the file. Zig then looks for the name in the current block, then the block around it, and so on out to the top of the file. If it reaches the top without finding it, you get this error. The Blocks section of the language reference opens with an example that ends in this very message. Names are case sensitive, so Message and message are different. Zig Scope explains how blocks limit where a name can be seen.
How to fix it
Section titled “How to fix it”Fix 1: Fix the spelling
Section titled “Fix 1: Fix the spelling”Copy the name from where it’s declared instead of retyping it:
const std = @import("std");
pub fn main() void { const message = "hi"; std.debug.print("{s}\n", .{message});}Output:
hiFix 2: Add the missing @import
Section titled “Fix 2: Add the missing @import”If the undeclared name is std, the file is missing its first line:
pub fn main() void { std.debug.print("Hello!\n", .{});}main.zig:2:5: error: use of undeclared identifier 'std' std.debug.print("Hello!\n", .{}); ^~~std isn’t built into the language. It’s a normal constant that you create with @import:
const std = @import("std");
pub fn main() void { std.debug.print("Hello!\n", .{});}Output:
Hello!The same goes for your own files: a function in math.zig is only visible after const math = @import("math.zig");, and then you call it as math.add(...). It must also be marked pub. Zig Modules covers this.
Fix 3: Declare the name outside the block
Section titled “Fix 3: Declare the name outside the block”A name made inside { ... } stops existing at the closing }:
const std = @import("std");
pub fn main() void { const age: u32 = 20; if (age >= 18) { const status = "adult"; } std.debug.print("{s}\n", .{status});}main.zig:6:15: error: unused local constant const status = "adult"; ^~~~~~main.zig:8:32: error: use of undeclared identifier 'status' std.debug.print("{s}\n", .{status}); ^~~~~~Zig sees two problems: inside the block status is never used, and outside it doesn’t exist. They’re really one problem. If you come from Python or JavaScript’s var, this is a change: in Zig, every { } block has its own names.
The neat fix in Zig is to make the if produce the value, so the name is declared at the level where you use it:
const std = @import("std");
pub fn main() void { const age: u32 = 20; const status = if (age >= 18) "adult" else "too young"; std.debug.print("{s}\n", .{status});}Output:
adultFor longer logic, declare a var before the block and assign it inside. The same rule applies to loop captures: |item| only exists inside its loop. See Zig If/Else.
Fix 4: Write self. for struct fields
Section titled “Fix 4: Write self. for struct fields”Inside a method, a field isn’t a name on its own. You reach it through self:
const std = @import("std");
const Counter = struct { count: u32,
fn bump(self: *Counter) void { count += 1; }};
pub fn main() void { var c: Counter = .{ .count = 0 }; c.bump(); std.debug.print("{d}\n", .{c.count});}main.zig:7:9: error: use of undeclared identifier 'count' count += 1; ^~~~~Zig has no hidden this, like Python has no hidden self. Write self.count += 1;:
const std = @import("std");
const Counter = struct { count: u32,
fn bump(self: *Counter) void { self.count += 1; }};
pub fn main() void { var c: Counter = .{ .count = 0 }; c.bump(); std.debug.print("{d}\n", .{c.count});}Output:
1Calling another method from inside a method works the same way: self.other(), not other(). See Zig Struct Methods.
Where you’ll usually see it
Section titled “Where you’ll usually see it”- Typos and wrong case, like
mesageorMessage. - A new file without
const std = @import("std");at the top. - A value set inside
if,for,whileor a block and used after it. - Methods that forget
self.in front of a field or another method. - Code from older tutorials that uses names that never existed in your file. If the name comes after a dot (like
std.io), you get a different error: root source file struct ‘x’ has no member named ‘y’.
Related errors
Section titled “Related errors”- root source file struct ‘x’ has no member named ‘y’: the name before the dot exists, but the part after it doesn’t.
- no field or member function named ‘x’: the value exists, but it has no such field or method.
- unused local variable: often appears together with this one, as in Fix 3.
- All Zig error pages: the full list.
Practice
Section titled “Practice”1. Ticket price
Section titled “1. Ticket price”This program should print price: 8. Fix both undeclared names, without moving the print line.
const std = @import("std");
pub fn main() void { const age: u32 = 12; if (age < 16) { const price: u32 = 8; } else { const price: u32 = 12; } std.debug.print("price: {d}\n", .{Price});}Hint: one is a capital letter. The other is a block problem: let the if give back the price.
Show solution
const std = @import("std");
pub fn main() void { const age: u32 = 12; const price: u32 = if (age < 16) 8 else 12; std.debug.print("price: {d}\n", .{price});}Output:
price: 8Each const price lived only inside its own block, so neither existed at the print. The if expression makes one price at the right level. The u32 isn’t strictly needed here, because age is known at compile time, but it keeps the line working if age later comes from user input: an if that picks between plain numbers at runtime needs a type.
- An identifier is a name. “Undeclared” means Zig can’t find it from where you used it.
- Check the spelling and case first. Zig doesn’t suggest a fix.
stdneedsconst std = @import("std");, and your own files need their own import.- Names live inside their
{ }block. Declare them where you use them, or letifproduce the value. - In methods, fields and other methods need
self..
Next up: expected type ‘X’, found ‘Y’, the error for a value of the wrong type.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Identifiers in the Zig language reference: which names are allowed.
- Blocks in the Zig language reference: how blocks limit where names can be seen.
- @import in the Zig language reference: how
stdand other files become names. - Zig Scope: our lesson on where names live.