0Pricing
Zig Academy · Lektion

Enums für eine feste Wertemenge

Benennen Sie eine abgeschlossene Liste von Optionen.

Enums für eine feste Wertemenge ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 1 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.

A Closed List of Choices

Some values come from a small, fixed menu: a traffic light is red, yellow, or green. An enum names exactly that closed list of options.

Declaring an Enum

You write enum and list the names you want inside braces. Each name is one valid value of that type.

const Color = enum { red, green, blue };

Enum Values Are Names

Each entry like red or blue is a real value of the type, not a string. You refer to it through the enum, keeping it tidy and typed.

const c: Color = Color.red;

The Dot Shorthand

When Zig already knows the expected type, you can drop the name and just write a dot before the value.

const c: Color = .green;

Comparing Enum Values

You compare enum values with == just like numbers. This makes branching on a fixed choice clean and readable.

if (c == .blue) {}

Only Listed Values Are Allowed

An enum can hold only the names you declared. Try to use one that does not exist and the compiler rejects it right away.

const c: Color = .purple; // error

Self-Documenting Code

Reading .green is far clearer than reading the number 1. Enums turn magic numbers into meaningful names you can trust.

Enums in Function Parameters

Accept an enum as a parameter and a caller can only pass one of your valid options, so bad input is caught at compile time.

fn paint(c: Color) void {}

Switch Pairs Well with Enums

Enums shine with switch: you handle each named case, and Zig reminds you if you forget one of the options.

switch (c) { .red => {}, .green => {}, .blue => {} }

Naming Conventions

Enum types use TitleCase like Color, while the individual values use lower snake_case like light_blue. This keeps the two roles distinct.

Why Reach for an Enum

Whenever a value is one of a few known states, an enum makes the choices explicit, safe, and impossible to misspell silently.

Quick Check

You want a type that is exactly one of red, green, or blue. What fits?

Recap

You met enums: a type that names a fixed set of values. Use the dot shorthand, compare with ==, and let the compiler block invalid choices. 🚦

Häufig gestellte Fragen

Ist die Lektion „Enums für eine feste Wertemenge“ kostenlos?

Ja — der vollständige Text von „Enums für eine feste Wertemenge“ 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 „Enums für eine feste Wertemenge“?

Benennen Sie eine abgeschlossene Liste von Optionen. 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 1 von 4.

Wie lange dauert die Lektion „Enums für eine feste Wertemenge“?

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

  1. Enums für eine feste Wertemenge
  2. Unions mit einem von mehreren Typen
  3. Markierte Unions und switch
  4. Enum-Methoden und zugrunde liegende Ganzzahlen
← Zurück zu Zig Academy