Typinferenz oder explizite Typen
Lassen Sie Zig den Typ ableiten oder geben Sie ihn selbst an.
Typinferenz oder explizite Typen ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 3 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.
Two Ways to Type
When you bind a value, Zig can figure out the type for you, or you can spell it out. Both rely on Zig's strong, static type system.
Inference for Clear Values
If the value makes the type obvious, let Zig infer it. const name = "Ada" is clearly text, so no annotation is needed.
const name = "Ada";Annotating Explicitly
To state a type yourself, write a colon and the type after the name. This is how you pin down an exact integer width.
const age: u8 = 30;Default Integer Type
An inferred integer literal becomes a comptime_int until you use it. When it lands in a slot, Zig coerces it to the needed width.
const small = 7; // comptime_intWhen You Must Annotate
Sometimes the value alone is not enough. A var starting at 0 needs an explicit type so Zig knows whether it is a u8 or an i64.
var counter: u32 = 0;Floats Need Care Too
A bare 3.14 is a comptime_float. Annotate it to choose f32 or f64 when precision and memory size actually matter to you.
const ratio: f32 = 3.14;Inference Aids Readability
When the type is obvious from context, dropping it keeps lines clean. Annotate only when it adds real clarity or is required.
Coercion Follows the Target
Assign 5 to a u16 slot and the literal quietly fits. Zig performs safe coercion toward the declared type whenever it can.
const n: u16 = 5;Explicit at Boundaries
At function signatures and data structures, types are always explicit. Inference is a local convenience, not a way to skip declarations.
fn area(w: u32, h: u32) u32 {}No Surprising Conversions
Zig will not silently turn a float into an int for you. Mismatched types raise an error, keeping numeric behavior predictable.
A Simple Habit
Infer when the value speaks for itself; annotate when the type is the information a reader needs. Clarity should drive the choice.
Quick Check
You write var counter = 0; and Zig complains. Why does it want more from you?
Recap
You saw that Zig can infer a type from a clear value, or you can annotate one with a colon. Let clarity and required cases decide which to use. 🎯
Häufig gestellte Fragen
Ist die Lektion „Typinferenz oder explizite Typen“ kostenlos?
Ja — der vollständige Text von „Typinferenz oder explizite 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 „Typinferenz oder explizite Typen“?
Lassen Sie Zig den Typ ableiten oder geben Sie ihn selbst an. 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 3 von 4.
Wie lange dauert die Lektion „Typinferenz oder explizite 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
- const für Werte, die sich nie ändern
- var für veränderlichen Zustand
- Typinferenz oder explizite Typen
- undefined und nicht initialisierter Speicher