0Pricing
Zig Academy · Lesson

Generic Data Structures

A type-returning function pattern.

Generic Data Structures is a free Zig Academy lesson on CoddyKit — lesson 2 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.

Generic Containers in Zig

To make a data structure work for any element type, you write a function that takes a type and returns a brand-new struct type. 📦

A Function That Returns a Type

The trick is that a function can have a return type of type. It computes and hands back a fresh type at compile time.

fn List(comptime T: type) type {
    return struct {};
}

Build the Struct Inside

Inside the function, define a struct whose fields use the type parameter T. Each call produces a struct tailored to that element type.

fn Box(comptime T: type) type {
    return struct { value: T };
}

Name the Resulting Type

Call the type-returning function and store the result in a const. Now you have a concrete type ready to use.

const IntBox = Box(i32);

Create an Instance

Use that named type just like any struct. Here we make a Box that holds the integer forty-two.

const b = IntBox{ .value = 42 };

Add Methods to the Inner Struct

The returned struct can contain methods too. They see the type parameter, so they stay fully generic.

fn Box(comptime T: type) type {
    return struct {
        value: T,
        fn get(self: @This()) T {
            return self.value;
        }
    };
}

Reach the Type with @This

Inside an anonymous returned struct you cannot name it directly, so methods use @This() to refer to the enclosing struct type.

self: @This()

Many Types from One Definition

Call the function with different types and you get distinct structs. Box(i32) and Box(f64) are separate, fully checked types.

const FloatBox = Box(f64);

This Powers the Standard Library

Zig's own ArrayList and HashMap are built this exact way: functions that take a type and return a configured container type.

const Ints = std.ArrayList(i32);

Resolved Entirely at Compile Time

All of this type construction happens during the build, so a generic container has the same cost as a hand-written one.

Carry an Allocator When Growing

Containers that grow store an allocator field, passed in at init, so the generic type stays explicit about where its memory comes from.

return struct {
    items: []T,
    alloc: std.mem.Allocator,
};

Quick Check

You want a generic stack that works for any element type in Zig. What pattern do you use?

Recap

Write a function that takes a type and returns a struct type. Each call yields a concrete, specialized container. This is how std builds its collections. 🎯

Frequently asked questions

Is the “Generic Data Structures” lesson free?

Yes — the full text of “Generic Data Structures” 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 “Generic Data Structures”?

A type-returning function pattern. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Generic Data Structures” 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. Functions That Take a Type
  2. Generic Data Structures
  3. @TypeOf and Type Reflection
  4. anytype Parameters
← Back to Zig Academy