Hello World

hello.zig

  1. const std = @import("std");
  2. pub fn main() !void {
  3. const stdout = std.io.getStdOut().outStream();
  4. try stdout.print("Hello, {}!\n", .{"world"});
  5. }
  1. $ zig build-exe hello.zig
  2. $ ./hello
  3. 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

  1. const warn = @import("std").debug.warn;
  2. pub fn main() void {
  3. warn("Hello, world!\n", .{});
  4. }
  1. $ zig build-exe hello.zig
  2. $ ./hello
  3. Hello, world!

Note that you can leave off the ! from the return type because warn cannot fail.

See also: