0Pricing
Zig Academy · Lesson

Declaring Structs and Fields

Group related data into a type.

Declaring Structs and Fields is a free Zig Academy lesson on CoddyKit — lesson 1 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.

Group Related Data

Loose variables get messy fast. A struct lets you bundle related values into one named type you can pass around together.

struct Is an Expression

In Zig a struct is created with the struct keyword and a body. You usually bind it to a const to give the type a name.

const Point = struct {};

Add Some Fields

Inside the braces you list fields, each with a name and a type. These describe the data every value of the struct holds.

const Point = struct {
    x: i32,
    y: i32,
};

Fields End With a Comma

Every field declaration ends with a comma, even the last one. Zig is strict here, so the formatter keeps it consistent.

const Color = struct {
    r: u8,
    g: u8,
    b: u8,
};

Make an Instance

To create a value you write the type name and a .{} initializer, setting each field by name.

const origin = Point{ .x = 0, .y = 0 };

Dot to Read a Field

Reach a field with a dot after the value. So origin.x reads the x field of that point.

const px = origin.x;

Mutating Needs var

To change a field, the instance must be a var. A const struct, and all its fields, stay immutable.

var p = Point{ .x = 1, .y = 2 };
p.x = 5;

Fields Can Be Any Type

A field can hold anything: a bool, a slice, even another struct. Structs nest freely to model richer shapes.

const Line = struct {
    start: Point,
    end: Point,
};

Order of Fields

In an ordinary struct Zig may reorder fields in memory for packing. You name fields, so layout never affects your code.

Structs Are Value Types

Assigning or passing a struct copies it by value. The original is untouched unless you deliberately use a pointer.

var a = Point{ .x = 1, .y = 1 };
var b = a; // b is a copy

A Type, Not a Variable

A struct definition describes a type, like a blueprint. Instances are the actual values you build from that blueprint.

Quick Check

How do you build a value from a struct type?

Recap

You met structs: the struct keyword groups named fields into a type, and Type{ .field = value } builds an instance. 🧱

Frequently asked questions

Is the “Declaring Structs and Fields” lesson free?

Yes — the full text of “Declaring Structs and Fields” 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 “Declaring Structs and Fields”?

Group related data into a type. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Declaring Structs and Fields” 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