Skip to content

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.

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.

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.

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:

hi

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.

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:

adult

For 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.

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:

1

Calling another method from inside a method works the same way: self.other(), not other(). See Zig Struct Methods.

  • Typos and wrong case, like mesage or Message.
  • A new file without const std = @import("std"); at the top.
  • A value set inside if, for, while or 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’.

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: 8

Each 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.
  • std needs const std = @import("std");, and your own files need their own import.
  • Names live inside their { } block. Declare them where you use them, or let if produce 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

1. A brand new file starts with pub fn main() and calls std.debug.print. What does Zig say?
2. const total is declared inside a for loop and printed after the loop. Why does Zig say it is undeclared?
3. Inside fn reset(self: *Timer) void, the line elapsed = 0; fails. What is the fix?