Hello World

hello.zig

  1. const std = @import("std");
  2. pub fn main() !void {
  3. const stdout = std.io.getStdOut().writer();
  4. try stdout.print("Hello, {s}!\n", .{"world"});
  5. }

Shell

  1. $ zig build-exe hello.zig
  2. $ ./hello
  3. Hello, world!

Most of the time, it is more appropriate to write to stderr rather than stdout, and whether or not the message is successfully written to the stream is irrelevant. For this common case, there is a simpler API:

hello_again.zig

  1. const std = @import("std");
  2. pub fn main() void {
  3. std.debug.print("Hello, world!\n", .{});
  4. }

Shell

  1. $ zig build-exe hello_again.zig
  2. $ ./hello_again
  3. Hello, world!

In this case, the ! may be omitted from the return type because no errors are returned from the function.

See also: