Curriculum Basics Hello and output
Hello and output
Two reflexes Zig wants you to internalise on day one.
- Imports use a builtin, not a keyword.
const std = @import("std");binds the standard library to a name. There's noimportkeyword and nousekeyword.@importis one of dozens of compiler builtins (everything starting with@) — you don't need the full list, just the muscle memory. maintakes aninit: std.process.Init. Zig's entry point used to be parameterless; modern Zig threads I/O capabilities (and soon, allocator handles) through anInitvalue the runtime constructs for you. You handinit.ioto anything that touches files / stdout / stderr. The signature ispub fn main(init: std.process.Init) !void— the!voidsays "may fail with an error".
Writing to stdout therefore reads as: get the stdout File handle, ask it to stream a slice of bytes through init.io.
try std.Io.File.stdout().writeStreamingAll(init.io, "hello\n");
try propagates the error if the write fails; !void in the main signature is what allows it. We'll come back to error unions properly in the errors module.
Exercises
9 ready
01
pick one
TypeScript pulls in a module with the import keyword. Zig has no
02
pick one
TypeScript's entry point is function main() { ... } (or just
03
pick one
Look at the canonical entry point: `pub fn main(init:
04
fill blanks
Fill the missing token. The program imports the Zig standard
05
fill blanks
Fill the missing keyword. The writeStreamingAll call returns an
06
type one line
Type the Zig line that writes hello from zig followed by a
07
type one line
Type the missing Zig line. The program should print two lines
08
write a program
Write a complete Zig program that prints exactly:
09
write a program
Write a complete Zig program that prints, on three separate