Skip to content

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.

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: admin
main.zig:3:14: note: struct declared here
const 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.

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.

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

For long literals, one field per line with a comma after the last one reads better, and zig fmt keeps it that way.

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: false
root: true

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

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 here
const 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 18

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

  • 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_path in writeFile).
  • Habits from C or JavaScript, where missing fields become zero or undefined on their own.

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 false

volume 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 struct first. undefined is 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

1. const p: Point = .{ .x = 1 }; and Point has x: i32, y: i32. What does Zig do?
2. How do you let callers leave out a field?
3. You add a field email to User, and five literals in your code now fail. Is that a problem?