Structs und Felder deklarieren
Gruppieren Sie zusammengehörige Daten zu einem Typ.
Structs und Felder 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.
Group Related Data
Loose variables get messy fast. A struct lets you bundle related values into one named type you can pass around together.
struct Is an Expression
In Zig a struct is created with the struct keyword and a body. You usually bind it to a const to give the type a name.
const Point = struct {};Add Some Fields
Inside the braces you list fields, each with a name and a type. These describe the data every value of the struct holds.
const Point = struct {
x: i32,
y: i32,
};Fields End With a Comma
Every field declaration ends with a comma, even the last one. Zig is strict here, so the formatter keeps it consistent.
const Color = struct {
r: u8,
g: u8,
b: u8,
};Make an Instance
To create a value you write the type name and a .{} initializer, setting each field by name.
const origin = Point{ .x = 0, .y = 0 };Dot to Read a Field
Reach a field with a dot after the value. So origin.x reads the x field of that point.
const px = origin.x;Mutating Needs var
To change a field, the instance must be a var. A const struct, and all its fields, stay immutable.
var p = Point{ .x = 1, .y = 2 };
p.x = 5;Fields Can Be Any Type
A field can hold anything: a bool, a slice, even another struct. Structs nest freely to model richer shapes.
const Line = struct {
start: Point,
end: Point,
};Order of Fields
In an ordinary struct Zig may reorder fields in memory for packing. You name fields, so layout never affects your code.
Structs Are Value Types
Assigning or passing a struct copies it by value. The original is untouched unless you deliberately use a pointer.
var a = Point{ .x = 1, .y = 1 };
var b = a; // b is a copyA Type, Not a Variable
A struct definition describes a type, like a blueprint. Instances are the actual values you build from that blueprint.
Quick Check
How do you build a value from a struct type?
Recap
You met structs: the struct keyword groups named fields into a type, and Type{ .field = value } builds an instance. 🧱
Häufig gestellte Fragen
Ist die Lektion „Structs und Felder deklarieren“ kostenlos?
Ja — der vollständige Text von „Structs und Felder 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 „Structs und Felder deklarieren“?
Gruppieren Sie zusammengehörige Daten zu einem Typ. 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 „Structs und Felder 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
- Structs und Felder deklarieren
- Methoden und der self-Parameter
- Standardwerte für Felder
- Anonyme Structs und Tupel