Unions mit einem von mehreren Typen
Speichern Sie alternative Formen in einem Platzhalter.
Unions mit einem von mehreren Typen 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.
One Slot, Many Shapes
Sometimes a value could be an integer or a float, but only one at a time. A union stores exactly one of several possible types in a single slot.
Declaring a Union
You write union and list named fields, each with its own type. The value holds just one of those fields at any moment.
const Number = union { int: i64, float: f64 };It Holds One Field at a Time
Unlike a struct, a union does not keep every field at once. It stores a single active field, and the others are not present.
Setting the Active Field
You initialize a union by choosing one field with the dot syntax. That field becomes the active one for the value.
var n = Number{ .int = 42 };Reading the Active Field
You read a union through the field you set. Access n.int and you get the integer you stored, just like reaching into a struct.
const x = n.int;Memory Is Shared
A union reserves room for its largest field and reuses that space. This makes it a compact way to hold one of several variants.
Switching the Active Field
Assigning a new field makes that field the active one and the old value goes away. The union now represents a different shape.
n = Number{ .float = 3.14 };Reading the Wrong Field
If you read a field that is not active, that is a bug. In safe builds Zig detects the wrong access and triggers a panic.
const f = n.int; // wrong field activeA Bare Union Has No Tag
A plain union does not remember which field is active. You must track that yourself, which is exactly the problem a tagged union solves.
Unions vs Structs
A struct is a logical AND of its fields, all present together. A union is a logical OR: just one field lives at a time.
When to Reach for a Union
Use a union when a value naturally takes one of a few forms, like a token that is a number or a symbol, never both at once.
Quick Check
How many of a union's fields hold a value at the same time?
Recap
You met unions: one slot that holds a single field from several typed options. They share memory, so set and read only the active field. 🔀
Häufig gestellte Fragen
Ist die Lektion „Unions mit einem von mehreren Typen“ kostenlos?
Ja — der vollständige Text von „Unions mit einem von mehreren Typen“ 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 „Unions mit einem von mehreren Typen“?
Speichern Sie alternative Formen in einem Platzhalter. 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 „Unions mit einem von mehreren Typen“?
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
- Enums für eine feste Wertemenge
- Unions mit einem von mehreren Typen
- Markierte Unions und switch
- Enum-Methoden und zugrunde liegende Ganzzahlen