Zig Error: missing struct field: x
error: missing struct field: age means you created a struct value with .{ ... } and left out a field that has no default value. Zig never fills in a field for you with zero or null: every field must be set, either in the literal or by a default in the struct’s declaration. If several are missing, the first one is in the error and the rest are listed as notes. For the other errors in this series, see Zig Errors Explained.
The error message
Section titled “The error message”const std = @import("std");
const User = struct { name: []const u8, age: u8, admin: bool,};
pub fn main() void { const u: User = .{ .name = "Ada" }; std.debug.print("{s}\n", .{u.name});}main.zig:10:22: error: missing struct field: age const u: User = .{ .name = "Ada" }; ~^~~~~~~~~~~~~~~~~main.zig:10:22: note: missing struct field: adminmain.zig:3:14: note: struct declared hereconst User = struct { ^~~~~~Read the notes too: admin is missing as well. The last note points at the struct’s declaration, where you can see every field and its type.
What it means
Section titled “What it means”A struct literal like .{ .name = "Ada" } builds a struct value by naming its fields. Zig checks the literal against the struct’s declaration: every field must get a value. In C, missing fields quietly become zero; in JavaScript, they become undefined. Zig prefers to stop and ask, because “I forgot this field” and “I want this field to be zero” look the same in those languages, and only one of them is a bug. If a field really has a sensible default, the reference’s Default Field Values section shows how to let a literal leave it out. Zig Structs explains literals and fields.
How to fix it
Section titled “How to fix it”Fix 1: Set every field
Section titled “Fix 1: Set every field”Most of the time you just add what’s missing:
const std = @import("std");
const User = struct { name: []const u8, age: u8, admin: bool,};
pub fn main() void { const u: User = .{ .name = "Ada", .age = 36, .admin = false }; std.debug.print("{s}, {d}, admin: {}\n", .{ u.name, u.age, u.admin });}Output:
Ada, 36, admin: falseFor long literals, one field per line with a comma after the last one reads better, and zig fmt keeps it that way.
Fix 2: Give the field a default value
Section titled “Fix 2: Give the field a default value”If most users aren’t admins, say so once, in the struct. A field with = value in the declaration can be left out of literals:
const std = @import("std");
const User = struct { name: []const u8, age: u8, admin: bool = false,};
pub fn main() void { const ada: User = .{ .name = "Ada", .age = 36 }; const root: User = .{ .name = "root", .age = 0, .admin = true }; std.debug.print("{s}: {}\n", .{ ada.name, ada.admin }); std.debug.print("{s}: {}\n", .{ root.name, root.admin });}Output:
Ada: falseroot: trueUse defaults for values that have a natural “normal” setting, like flags, counters starting at 0, or optional fields that start as null (nickname: ?[]const u8 = null). Don’t add a default just to silence the error: a User without a real name is probably a bug.
Fix 3: Check the field name
Section titled “Fix 3: Check the field name”A misspelled field gives a different error first. Zig doesn’t know .nmae, so it says so:
const std = @import("std");
const User = struct { name: []const u8, age: u8,};
pub fn main() void { const u: User = .{ .nmae = "Ada", .age = 36 }; std.debug.print("{s}\n", .{u.name});}main.zig:9:25: error: no field named 'nmae' in struct 'main.User' const u: User = .{ .nmae = "Ada", .age = 36 }; ^~~~main.zig:3:14: note: struct declared hereconst User = struct { ^~~~~~Fix the spelling, and if name was the only problem, the program builds. If you fix it and then see missing struct field: name, the field really was left out.
Fix 4: undefined when you’ll fill it in later
Section titled “Fix 4: undefined when you’ll fill it in later”Sometimes you really will set a field a few lines later, for example in a loop. Then you can say so with undefined:
const std = @import("std");
const Stats = struct { count: u32, total: u32,};
pub fn main() void { const nums = [_]u32{ 4, 6, 8 }; var s: Stats = .{ .count = nums.len, .total = undefined }; s.total = 0; for (nums) |n| s.total += n; std.debug.print("{d} numbers, total {d}\n", .{ s.count, s.total });}Output:
3 numbers, total 18undefined means “no meaningful value yet”. Reading it before you set it is a bug that Zig may not catch (in debug builds the memory is filled with 0xaa bytes, so you’d see odd numbers). Here, .total = 0 would be simpler and safer. Reach for undefined only when there’s no sensible starting value, like a large buffer you’re about to fill. See Zig Variables.
Where you’ll usually see it
Section titled “Where you’ll usually see it”- After adding a field to a struct that’s built in several places.
- Big config structs where you only care about a few fields. Give the rest defaults.
- Standard library option structs: their fields usually have defaults, so
.{}works. If one doesn’t, the error names the field you must pass (for example.sub_pathinwriteFile). - Habits from C or JavaScript, where missing fields become zero or
undefinedon their own.
Related errors
Section titled “Related errors”- expected N argument(s), found M: the function version, an argument left out of a call.
- no field or member function named ‘x’: reading a field that doesn’t exist.
- use of undeclared identifier ‘x’: a field used without
self.inside a method. - All Zig error pages: the full list.
Practice
Section titled “Practice”1. Game settings
Section titled “1. Game settings”Most settings should have sensible defaults, so that .{ .player = "Ada" } builds. Change the struct (not the literal) so the program prints Ada: volume 80, hard mode false.
const std = @import("std");
const Settings = struct { player: []const u8, volume: u8, hard_mode: bool,};
pub fn main() void { const s: Settings = .{ .player = "Ada" }; std.debug.print("{s}: volume {d}, hard mode {}\n", .{ s.player, s.volume, s.hard_mode });}Hint: give volume and hard_mode default values. Should player have one?
Show solution
const std = @import("std");
const Settings = struct { player: []const u8, volume: u8 = 80, hard_mode: bool = false,};
pub fn main() void { const s: Settings = .{ .player = "Ada" }; std.debug.print("{s}: volume {d}, hard mode {}\n", .{ s.player, s.volume, s.hard_mode });}Output:
Ada: volume 80, hard mode falsevolume and hard_mode have natural defaults. player doesn’t, so it stays required: forgetting it is still an error, which is what you want.
- Every struct field must get a value: in the literal, or from a default in the declaration.
- Zig never fills a field with zero or null on its own.
- The first missing field is in the error, the rest are in notes.
- Use defaults (
admin: bool = false) for fields with a natural normal value; keep important ones required. - A typo gives
no field named 'x' in structfirst.undefinedis for “I’ll set it in a moment”.
Next up: switch must handle all possibilities, the error for a switch that misses a case.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- struct in the Zig language reference: declaring fields and struct literals.
- Default field values in the Zig language reference: when defaults are a good idea.
- undefined in the Zig language reference: what
undefinedmeans. - Zig Structs: our lesson on structs and literals.