var für veränderlichen Zustand
Wann und wie Sie eine Neuzuweisung erlauben.
var für veränderlichen Zustand 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.
When Values Change
Some values truly need to move: a counter, a running total, a current score. For those, Zig gives you the var keyword.
Meet var
The keyword var creates a mutable binding. Unlike const, the name you declare with var can be reassigned again and again.
var score = 0;Reassigning Freely
Once a value is a var, updating it is just a plain assignment. No special syntax is needed, only the name and a new value.
var score = 0;
score = 10;Updating in Place
You can compute a new value from the old one. Adding to a counter is the classic example of useful mutation in a loop.
var count: u32 = 0;
count += 1;Type Often Needed
For a var that starts at zero, Zig may ask for a type so it knows the integer width. Annotate it once and you are set.
var total: i64 = 0;Unused Mutation Warns
If you declare a var but never actually change it, Zig warns you. It nudges you to use const instead for a clearer intent.
var Inside Functions
Mutable locals live inside functions and last for that scope. A loop accumulator declared with var is a perfect everyday use.
var sum: u32 = 0;
sum += value;The Cost of Mutation
Every var is one more thing that can change underfoot. Use it where change is the point, not as a lazy default choice.
Loop Counters
A while loop often drives a var forward each pass. The binding updates, the condition rechecks, and the loop advances.
var i: usize = 0;
while (i < 5) : (i += 1) {}Same Type, New Value
A var can change its value but never its type. An i32 stays an i32; you cannot suddenly store text in a number binding.
var vs const Recap
Think of it simply: const is a value pinned in place, while var is a value that is meant to move. Reach for var only when it must.
Quick Check
You need a counter that increases inside a loop. Which keyword should declare it?
Recap
You learned that var creates a mutable binding for values that change. Annotate its type when needed, and use it only where mutation is truly the goal. 🔁
Häufig gestellte Fragen
Ist die Lektion „var für veränderlichen Zustand“ kostenlos?
Ja — der vollständige Text von „var für veränderlichen Zustand“ 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 „var für veränderlichen Zustand“?
Wann und wie Sie eine Neuzuweisung 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 2 von 4.
Wie lange dauert die Lektion „var für veränderlichen Zustand“?
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