Generische Datenstrukturen
Ein Muster für eine Funktion, die einen Typ zurückgibt.
Generische Datenstrukturen ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Zig Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎯
Häufig gestellte Fragen
Ist die Lektion „Generische Datenstrukturen“ kostenlos?
Ja — der vollständige Text von „Generische Datenstrukturen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Zig Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Generische Datenstrukturen“?
Ein Muster für eine Funktion, die einen Typ zurückgibt. Du übst Zig Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Zig Academy zu starten?
Keine Vorkenntnisse erforderlich. Zig Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Generische Datenstrukturen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Zig Academy-Lektion Code schreiben und ausführen?
Ja. Jede Zig Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Funktionen, die einen Typ übernehmen
- Generische Datenstrukturen
- @TypeOf und Typreflexion
- anytype-Parameter