Hello World
hello.zig
const std = @import("std");
pub fn main() !void {
const stdout = std.io.getStdOut().outStream();
try stdout.print("Hello, {}!\n", .{"world"});
}
$ zig build-exe hello.zig
$ ./hello
Hello, world!
Usually you don't want to write to stdout. You want to write to stderr. And you don't care if it fails. It's more like a warning message that you want to emit. For that you can use a simpler API:
hello.zig
const warn = @import("std").debug.warn;
pub fn main() void {
warn("Hello, world!\n", .{});
}
$ zig build-exe hello.zig
$ ./hello
Hello, world!
Note that you can leave off the !
from the return type because warn
cannot fail.
See also: