0Pricing
Zig Academy · Lesson

Anonymous Structs and Tuples

Create ad-hoc grouped values.

Anonymous Structs and Tuples is a free Zig Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Sometimes You Need It Once

Not every grouping deserves a named type. Zig lets you write an anonymous struct inline, right where you use it.

Just Drop the Name

An anonymous struct literal starts with .{ and lists fields by name, with no type written before it.

const p = .{ .x = 1, .y = 2 };

Use the Fields Normally

You read fields the same way as a named struct, with a dot. Here p.x is 1 and p.y is 2.

const sum = p.x + p.y;

Coerce to a Named Type

An anonymous literal can coerce into a named struct when the target type is known, like a return value.

fn make() Point {
    return .{ .x = 0, .y = 0 };
}

Tuples Have No Field Names

Leave out the field names and you get a tuple: an anonymous struct whose fields are accessed by position.

const pair = .{ 1, 2 };

Index a Tuple

Reach a tuple field by number, written like a field name. So pair[0] is 1 and pair[1] is 2.

const first = pair[0];

Mixed Types Are Fine

Tuple fields can each be a different type. One literal can hold an int, a bool, and a string at once.

const row = .{ 42, true, "hi" };

Tuples Have a Length

A tuple is still a struct, so it has a compile-time len you can read with tuple.len.

const n = row.len; // 3

Great for print Arguments

std.debug.print takes a tuple of values to format. That .{} you pass is an anonymous tuple.

std.debug.print("{} {}\n", .{ 1, 2 });

Loop Over a Tuple

Because the length is known at compile time, you can walk a tuple's fields with an inline for loop.

inline for (row) |field| {
    _ = field;
}

Named or Positional

Reach for an anonymous struct when names add clarity, and a tuple when a short, ordered group is all you need.

Quick Check

What is the difference between a tuple and a named anonymous struct?

Recap

You met anonymous structs and tuples: .{ .x = 1 } is inline and named, while .{ 1, 2 } is a positional tuple. 📦

Frequently asked questions

Is the “Anonymous Structs and Tuples” lesson free?

Yes — the full text of “Anonymous Structs and Tuples” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “Anonymous Structs and Tuples”?

Create ad-hoc grouped values. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Anonymous Structs and Tuples” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Zig Academy lesson?

Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Declaring Structs and Fields
  2. Methods and the self Parameter
  3. Default Field Values
  4. Anonymous Structs and Tuples
← Back to Zig Academy