0PricingLogin
Zig Academy · Lesson

Generic Data Structures

A type-returning function pattern.

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 {};
}

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