Skip to content

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.

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:

  1. 'push' is the name you used.
  2. 'array_list.Aligned(i32,null)' is the type Zig looked in. That’s the full name of std.ArrayList(i32): ArrayList is 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.

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.

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

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

3

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

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.

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

  • 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 says optional type '?T' does not support field access instead. Unwrap it first (if (x) |v| v.len), see Zig Optionals.

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

Inside 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.x looks 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.
  • .len is a field: no (). A missing field gives no 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

1. Which line gets the length of const s: []const u8 = "zig";?
2. fn total(c: Cart) u32 is declared after const Cart = struct { ... };. Why does cart.total() fail?
3. What is the Zig name for adding an item to an ArrayList?