0Pricing
Zig Academy · Lektion

Optionale Typen deklarieren

Fügen Sie ? hinzu, um einen fehlenden Wert zu erlauben.

Optionale Typen deklarieren 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.

Sometimes There Is Nothing

Real programs hit cases where a value is simply absent: a lookup misses, a field is empty. Zig models this absence safely and on purpose.

The Optional Type

An optional type can hold either a real value of some type, or the special value null meaning nothing is there.

Add a Leading Question Mark

You make any type optional by writing ? in front of it. So ?i32 is an integer that might also be null.

var age: ?i32 = null;

A Value Fits Right In

An optional happily holds a normal value too. You assign 42 to an ?i32 exactly as you would assign to a plain i32.

var age: ?i32 = 42;

null Is the Empty State

The keyword null is the literal that means there is no value. Only optional types are allowed to hold it.

const missing: ?u8 = null;

Non-Optionals Reject null

A plain i32 can never be null. The compiler stops you, so absence stays visible in the type rather than hiding as a bad value.

var n: i32 = null; // error: expected i32

Wrap Any Type You Like

Optionals are not just for numbers. You can mark a bool, a struct, a slice, or any type as optional by adding the question mark.

var found: ?bool = null;

Inference Still Works

Assign an optional value and Zig infers the optional type for you, so you do not always have to spell it out by hand.

const score: ?u32 = 10;

Why Not Use a Sentinel

In C people fake absence with values like -1 or a null pointer. Zig gives you a real optional so absence cannot be confused with data.

Absence Is Part of the Type

Because ? lives in the type itself, anyone reading your code instantly sees that a value might be missing and must be handled.

Defaulting to null

It is common to start an optional at null and fill it in later once you actually have the value to store.

var result: ?[]const u8 = null;

Quick Check

How do you declare a u32 that might be missing?

Recap

You met optionals: write ? before a type to allow a real value or null. Absence now lives in the type, plain and visible. ❓

Häufig gestellte Fragen

Ist die Lektion „Optionale Typen deklarieren“ kostenlos?

Ja — der vollständige Text von „Optionale Typen deklarieren“ 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 „Optionale Typen deklarieren“?

Fügen Sie ? hinzu, um einen fehlenden Wert zu erlauben. 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 „Optionale Typen deklarieren“?

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. Optionale Typen deklarieren
  2. Sicher entpacken mit if und orelse
  3. Optionale Zeiger und ?*T
  4. null im Vergleich zu undefined
← Zurück zu Zig Academy