Zig Error: no field or member function named 'x' in 'T'
error: no field or member function named 'x' in 'T' means you wrote value.x or value.x(), and the type T has no field and no method called x. The type in the quotes is what Zig thinks value is, so start there. Most of the time the name comes from another language (push instead of append), the function isn’t inside the struct, or you called a field like .len as if it were a method. 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(init: std.process.Init) !void { var list: std.ArrayList(i32) = .empty; defer list.deinit(init.gpa); try list.push(init.gpa, 1);}main.zig:6:13: error: no field or member function named 'push' in 'array_list.Aligned(i32,null)' try list.push(init.gpa, 1); ~~~~^~~~~.../lib/std/array_list.zig:638:12: note: struct declared here return struct { ^~~~~~Two things to read:
'push'is the name you used.'array_list.Aligned(i32,null)'is the type Zig looked in. That’s the full name ofstd.ArrayList(i32):ArrayListis a short alias for it. The note shows where that type is declared, which is the place to look for the names it does have.
What it means
Section titled “What it means”When you write value.name, Zig looks for name in two places: the fields of value’s type, and the functions declared inside that type (its methods). If neither has it, you get this error. Zig never looks anywhere else: there are no extension methods, no inheritance and no methods added at runtime. That makes the error precise. The name is missing from that one type. The struct section of the language reference shows both kinds of member: fields, and functions declared inside the struct. Zig Struct Methods explains how methods belong to a type.
How to fix it
Section titled “How to fix it”Fix 1: Find the real name
Section titled “Fix 1: Find the real name”If the type comes from the standard library, the method probably exists under another name. ArrayList uses append, not push or add:
const std = @import("std");
pub fn main(init: std.process.Init) !void { var list: std.ArrayList(i32) = .empty; defer list.deinit(init.gpa); try list.append(init.gpa, 1); try list.append(init.gpa, 2); std.debug.print("{any}\n", .{list.items});}Output:
{ 1, 2 }The fastest way to see what a type has is your editor: type list. and ZLS lists every field and method. The other is the source: the note’s path points into lib/std/, which ships with Zig, and every pub fn in that struct is a method you can call. Names change between Zig versions, so code from an older tutorial is a common cause. For example, getLast() on ArrayList is deprecated in 0.17 in favour of last().
| From other languages | In Zig |
|---|---|
list.push(x), list.add(x) |
try list.append(gpa, x) |
list.length, list.size() |
list.items.len |
text.length, text.len() |
text.len |
text.contains("x") |
std.mem.find(u8, text, "x") != null |
Fix 2: Put the function inside the struct
Section titled “Fix 2: Put the function inside the struct”A function only becomes a method if it’s declared inside the struct’s { }:
const std = @import("std");
const Point = struct { x: i32, y: i32,};
fn length(p: Point) i32 { return p.x + p.y;}
pub fn main() void { const p: Point = .{ .x = 1, .y = 2 }; std.debug.print("{d}\n", .{p.length()});}main.zig:14:33: error: no field or member function named 'length' in 'main.Point' std.debug.print("{d}\n", .{p.length()}); ~^~~~~~~main.zig:3:15: note: struct declared hereconst Point = struct { ^~~~~~length exists, but it’s a free function next to Point, not part of it. Move it in, with the struct as its first parameter:
const std = @import("std");
const Point = struct { x: i32, y: i32,
fn length(self: Point) i32 { return self.x + self.y; }};
pub fn main() void { const p: Point = .{ .x = 1, .y = 2 }; std.debug.print("{d}\n", .{p.length()});}Output:
3p.length() is now short for Point.length(p). (If you’d rather keep a free function, call it as length(p).) If the struct is in another file, the method must also be pub.
Fix 3: .len is a field, not a method
Section titled “Fix 3: .len is a field, not a method”Slices and arrays have a len field. There’s no len() method:
const std = @import("std");
pub fn main() void { const name: []const u8 = "Ada"; std.debug.print("{d}\n", .{name.len()});}main.zig:5:36: error: no field or member function named 'len' in '[]const u8' std.debug.print("{d}\n", .{name.len()}); ~~~~^~~~The message is a bit misleading here, since len is a field. What Zig means is that there’s no len you can call. Drop the parentheses: name.len. The length is in bytes, by the way: see Zig Strings in Depth.
Fix 4: Fields have their own message
Section titled “Fix 4: Fields have their own message”Reading a field that doesn’t exist (no parentheses) gives a shorter message:
const std = @import("std");
const Point = struct { x: i32, y: i32,};
pub fn main() void { const p: Point = .{ .x = 1, .y = 2 }; std.debug.print("{d}\n", .{p.z});}main.zig:10:34: error: no field named 'z' in struct 'main.Point' std.debug.print("{d}\n", .{p.z}); ^main.zig:3:15: note: struct declared hereconst Point = struct { ^~~~~~Check the struct’s declaration (the note points to it) for the right field name, or add the field if it’s missing. On a slice, name.length gives no member named 'length' in '[]const u8'; the fix is name.len.
Where you’ll usually see it
Section titled “Where you’ll usually see it”- Method names from Python, JavaScript, Java or Rust:
push,add,size(),length,contains. - Code from an older Zig version, where a method was renamed or removed.
- Helper functions declared after the struct instead of inside it.
- Optionals: on a
?T, Zig saysoptional type '?T' does not support field accessinstead. Unwrap it first (if (x) |v| v.len), see Zig Optionals.
Related errors
Section titled “Related errors”- root source file struct ‘x’ has no member named ‘y’: the same kind of miss, on a file or namespace like
std.io. - use of undeclared identifier ‘x’: a field used inside a method without
self.. - expected N argument(s), found M: the method exists, but you passed the wrong number of arguments.
- All Zig error pages: the full list.
Practice
Section titled “Practice”1. Make it a method
Section titled “1. Make it a method”Fix this program so r.area() works and it prints area: 12.
const std = @import("std");
const Rect = struct { w: u32, h: u32,};
fn area(r: Rect) u32 { return r.w * r.h;}
pub fn main() void { const r: Rect = .{ .w = 3, .h = 4 }; std.debug.print("area: {d}\n", .{r.area()});}Hint: where must area be declared for r.area() to find it?
Show solution
const std = @import("std");
const Rect = struct { w: u32, h: u32,
fn area(self: Rect) u32 { return self.w * self.h; }};
pub fn main() void { const r: Rect = .{ .w = 3, .h = 4 }; std.debug.print("area: {d}\n", .{r.area()});}Output:
area: 12Inside the struct, area is a method, so r.area() passes r as self. Calling the free function as area(r) would have worked too.
value.xlooks only in the fields and methods declared inside the value’s type.- The quoted type tells you where Zig looked. Check it’s the type you expected.
- Standard library names differ from other languages and between Zig versions:
append,items.len,.len. - A function is a method only if it’s inside the struct.
.lenis a field: no(). A missing field givesno field named 'x' in struct.
Next up: root source file struct ‘x’ has no member named ‘y’, the error for a missing name in a file like std.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- struct in the Zig language reference: fields, methods and declarations inside a struct.
- Slices in the Zig language reference:
ptrandlen. - 0.17.0 release notes: renamed and removed standard library functions.
- Zig Struct Methods: our lesson on methods and
self.