Strings sind []const u8
Warum Zig-Strings Byte-Slices sind.
Strings sind []const u8 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.
No String Type
Zig has no dedicated string type. A piece of text is simply a slice of bytes, so you reuse the same tools you already know for arrays. 📦
It Is Just Bytes
A Zig string is the type []const u8: a read-only window over a run of 8-bit bytes. Each byte holds one number from 0 to 255.
const greeting: []const u8 = "hello";Why const
String literals live in read-only memory, so their type is []const u8. The const means you can read the bytes but never write through this slice.
u8 Is One Byte
The element type u8 is an unsigned 8-bit integer. Treating text as u8 makes Zig honest: a string is raw bytes, not magic characters.
A Slice Has a Length
A slice carries both a pointer and a length, so the string knows its own size. You never need a separate length variable or a terminator.
const name = "Zig";
// name.len == 3UTF-8 by Convention
Zig source files are UTF-8, so literals store text as UTF-8 bytes. One emoji or accented letter may take several bytes, not one.
Length Counts Bytes
Because text is bytes, .len returns the byte count, not the character count. For plain ASCII the two numbers match exactly.
const s = "abc";
std.debug.print("{}", .{s.len}); // 3Index Returns a Byte
Indexing a string gives you back a single u8 byte value, not a character object. For ASCII, that byte is the letter is code.
const s = "AB";
const first = s[0]; // 65, the ANo Null Terminator Needed
Unlike C, a Zig slice does not rely on a trailing zero byte to mark its end. The stored length already tells code where text stops.
Print a String
You print text with the {s} format specifier, which tells Zig to render the bytes as a string instead of as numbers.
std.debug.print("{s}\n", .{"hello"});Mutable Bytes Need [] u8
Drop the const to get a writable []u8 slice. Then you can change bytes in place, as long as they live in memory you own.
Quick Check
Think about the real type behind a Zig string literal.
Recap
You learned that Zig text is just []const u8: read-only bytes with a length, UTF-8 by convention, indexed and measured in bytes. 🎯
Häufig gestellte Fragen
Ist die Lektion „Strings sind []const u8“ kostenlos?
Ja — der vollständige Text von „Strings sind []const u8“ 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 „Strings sind []const u8“?
Warum Zig-Strings Byte-Slices sind. 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 „Strings sind []const u8“?
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
- Strings sind []const u8
- String-Literale und Escape-Sequenzen
- Bytes vergleichen und durchsuchen
- Text mit std.fmt formatieren