Curriculum Basics Hello and output exercise 2 · mcq
Hello and output
TypeScript's entry point is function main() { ... } (or just
module-top code). Zig's main has a specific signature that
modern programs are expected to use — pick the one that's
callable from the compiler-generated runtime AND lets you
try an I/O call.
TypeScript reference
About this theme
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.