Zig Result Location Semantics
You’ve been writing things like const p: Point = .{ .x = 1, .y = 2 };, var list: std.ArrayList(u8) = .empty; and const small: u8 = @intCast(big); for a while now. In each one, the right-hand side doesn’t name a type, yet Zig knows exactly what to build. The rule behind this has a big name, result location semantics, but the idea is small: an expression can see where its value is going. This short lesson explains that idea, and the one surprise it causes.
Result types: the type flows in from the left
Section titled “Result types: the type flows in from the left”When you write const x: u32 = 42;, the u32 on the left is passed into the expression on the right. Zig calls it the expression’s result type. The literal 42 doesn’t have to guess what it is: it’s told.
That’s how a bare .{ ... } works. It has no type of its own, so it takes the result type from wherever it’s going:
const std = @import("std");
const Point = struct { x: i32, y: i32,};
fn show(p: Point) void { std.debug.print("({d}, {d})\n", .{ p.x, p.y });}
fn middle() Point { return .{ .x = 5, .y = 5 };}
pub fn main() void { const a: Point = .{ .x = 1, .y = 2 }; show(a); show(.{ .x = 3, .y = 4 }); show(middle());}Output:
(1, 2)(3, 4)(5, 5)Three places, one rule:
const a: Point = .{ ... }: the type annotation gives the result type.show(.{ ... }): the parameter type ofshowgives it.return .{ ... }: the function’s return type gives it.
It keeps going inward, too: each field value gets the field’s type as its result type. So in .{ .x = 1 }, the 1 knows it’s an i32.
Decl literals: .empty, .init(...)
Section titled “Decl literals: .empty, .init(...)”The same trick works for names. When the result type is known, .something means “the declaration called something inside that type”. The Zig team calls these decl literals:
const std = @import("std");
const Point = struct { x: i32, y: i32,
const origin: Point = .{ .x = 0, .y = 0 };
fn init(x: i32, y: i32) Point { return .{ .x = x, .y = y }; }};
pub fn main() void { const a: Point = .origin; const b: Point = .init(7, 8); std.debug.print("a = ({d}, {d}), b = ({d}, {d})\n", .{ a.x, a.y, b.x, b.y });}Output:
a = (0, 0), b = (7, 8).origin is short for Point.origin, and .init(7, 8) is short for Point.init(7, 8). Now you know what’s happening in lines you’ve been copying: var list: std.ArrayList(u8) = .empty; uses the empty declaration inside ArrayList(u8), and var arena: std.heap.ArenaAllocator = .init(init.gpa); calls ArenaAllocator.init. The type is written once, on the left, and the right side stays short.
Casts use the result type too
Section titled “Casts use the result type too”Casting builtins like @intCast don’t take a target type. They use the result type:
const std = @import("std");
pub fn main() void { const big: u32 = 300; const small = @intCast(big); std.debug.print("{d}\n", .{small});}main.zig:5:19: error: @intCast must have a known result type const small = @intCast(big); ^~~~~~~~~~~~~main.zig:5:19: note: use @as to provide explicit result typeconst small = ... has no annotation, so there’s no result type, and @intCast doesn’t know what to cast to. Give it one, either with an annotation or with @as, which exists exactly to provide a result type:
const std = @import("std");
pub fn main() void { const big: u32 = 200; const a: u8 = @intCast(big); const b = @as(u8, @intCast(big)); std.debug.print("{d} {d}\n", .{ a, b });}Output:
200 200Both lines mean the same thing. Most Zig code prefers the annotation; @as is handy in the middle of a bigger expression. (@intCast also checks that the value fits. Our 300 is known while compiling, so const small: u8 = @intCast(big); would be a compile error: “type ‘u8’ cannot represent integer value ‘300’”. For values only known while running, a debug build panics instead. See Zig Type Casting.)
No result type, no magic
Section titled “No result type, no magic”When nothing tells the expression its type, the shortcuts stop working. A bare .empty with no annotation is just an enum literal:
pub fn main() void { var list = .empty; _ = &list;}main.zig:2:9: error: variable of type '@EnumLiteral()' must be const or comptime var list = .empty; ^~~~And a bare .{ .x = 1, .y = 2 } with no result type becomes its own anonymous struct type. It doesn’t turn into a Point later, even though the fields match:
const Point = struct { x: i32, y: i32 };
pub fn main() void { const p = .{ .x = 1, .y = 2 }; const q: Point = p; _ = q;}main.zig:5:22: error: expected type 'main.Point', found 'main.main__struct_789' const q: Point = p; ^The fix in both cases is the same: put the type on the left, var list: std.ArrayList(u8) = .empty; or const p: Point = .{ ... };. If you hit an error that mentions @EnumLiteral() or a strange __struct_ name, a missing type annotation is the first thing to check.
Result locations: built right where it goes
Section titled “Result locations: built right where it goes”That’s the “type” half. The other half is the location. When you assign p = .{ .x = ..., .y = ... }, Zig doesn’t build a temporary struct somewhere and then copy it into p. It writes each field straight into p: first p.x, then p.y. The language reference on result locations describes it exactly like that: the assignment becomes p.x = ...; p.y = ...;.
Most of the time you’ll never notice, and it saves copies. But it has one surprise. Try swapping two fields:
const std = @import("std");
const Point = struct { x: i32, y: i32 };
pub fn main() void { var p: Point = .{ .x = 1, .y = 2 }; p = .{ .x = p.y, .y = p.x }; std.debug.print("x = {d}, y = {d}\n", .{ p.x, p.y });}Output:
x = 2, y = 2Not a swap. Step by step: p.x = p.y sets x to 2. Then p.y = p.x reads the new x, which is already 2. The same thing happens with arrays (a = .{ a[1], a[0] };) and with tuples, as you saw in Zig Anonymous Structs & Tuples.
The fix is to read the old values first, or use std.mem.swap:
const std = @import("std");
const Point = struct { x: i32, y: i32 };
pub fn main() void { var p: Point = .{ .x = 1, .y = 2 }; const old = p; p = .{ .x = old.y, .y = old.x }; std.debug.print("x = {d}, y = {d}\n", .{ p.x, p.y });
var q: Point = .{ .x = 1, .y = 2 }; std.mem.swap(i32, &q.x, &q.y); std.debug.print("x = {d}, y = {d}\n", .{ q.x, q.y });}Output:
x = 2, y = 1x = 2, y = 1const old = p; is a full copy, so reading old.y and old.x isn’t affected by writing into p.
Practice
Section titled “Practice”1. Let the type flow
Section titled “1. Let the type flow”This code doesn’t compile. Fix it by adding one type annotation, without changing the right-hand side.
const std = @import("std");
const Size = struct { w: u32, h: u32 };
pub fn main() void { const s = .{ .w = 4, .h = 3 }; const area: u32 = s.w * s.h; const box: Size = s; std.debug.print("{d} {d}\n", .{ area, box.w });}Hint: if s is a Size from the start, the last assignment is just a copy.
Show solution
const std = @import("std");
const Size = struct { w: u32, h: u32 };
pub fn main() void { const s: Size = .{ .w = 4, .h = 3 }; const area: u32 = s.w * s.h; const box: Size = s; std.debug.print("{d} {d}\n", .{ area, box.w });}Output:
12 4With : Size, the literal is built as a Size instead of an anonymous struct, so it can be assigned to box.
2. Rotate three values
Section titled “2. Rotate three values”var a: [3]u8 = .{ 1, 2, 3 }; should become { 2, 3, 1 }. Do it with one array assignment that reads from a copy.
Hint: const old = a; then a = .{ old[1], old[2], old[0] };.
Show solution
const std = @import("std");
pub fn main() void { var a: [3]u8 = .{ 1, 2, 3 }; const old = a; a = .{ old[1], old[2], old[0] }; std.debug.print("{any}\n", .{a});}Output:
{ 2, 3, 1 }Without the copy, a = .{ a[1], a[2], a[0] }; would give { 2, 3, 2 }: by the time it reads a[0], it’s already been overwritten.
- Every expression can get a result type from where its value goes: an annotation, a parameter, a return type or a field.
- That’s why
.{ ... }needs no type name, and why decl literals like.empty,.originand.init(...)work. - Casts like
@intCastuse the result type; give one with an annotation or@as. - No result type means an enum literal or an anonymous struct, which won’t turn into your struct later.
- Values are written straight into their destination, field by field, so
p = .{ .x = p.y, .y = p.x }doesn’t swap. Copy first, or usestd.mem.swap.
Next up: Zig Errors, how Zig reports things going wrong with error sets and !T.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Zig language reference: Result Location Semantics: the official rules, with tables of how each kind of expression passes types and locations down.
- Zig language reference: Result Types: how casts like
@intCastuse them. - Zig 0.14.0 release notes: Decl Literals: where
.init(...)and.emptycame from, and why. - Zig language reference: Anonymous Struct Literals: what
.{ ... }becomes when there’s no result type.