#
Zig Language Reference
#
Introduction
Zig is a general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
RobustBehavior is correct even for edge cases such as out of memory.OptimalWrite programs the best way they can behave and perform.ReusableThe same code works in many environments which have different constraints.MaintainablePrecisely communicate intent to the compiler and other programmers. The language imposes a low overhead to reading code and is resilient to changing requirements and environments.
Often the most efficient way to learn something new is to see examples, so this documentation shows how to use each of Zig's features. It is all on one page so you can search with your browser's search tool.
The code samples in this document are compiled and tested as part of the main test suite of Zig.
This HTML document depends on no external files, so you can use it offline.
#
Zig Standard Library
The Zig Standard Library has its own documentation.
Zig's Standard Library contains commonly used algorithms, data structures, and definitions to help you build programs or libraries. You will see many examples of Zig's Standard Library used in this documentation. To learn more about the Zig Standard Library, visit the link above.
Alternatively, the Zig Standard Library documentation is provided with each Zig distribution. It can be rendered via a local webserver with:
Shell
zig std
#
Hello World
const std = @import("std");
pub fn main() !void {
try std.fs.File.stdout().writeAll("Hello, World!\n");
}
Shell
$ zig build-exe hello.zig
$ ./hello
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. Also, formatted printing often comes in handy. For this common case, there is a simpler API:
const std = @import("std");
pub fn main() void {
std.debug.print("Hello, {s}!\n", .{"World"});
}
Shell
$ zig build-exe hello_again.zig
$ ./hello_again
Hello, World!
In this case, the ! may be omitted from the return
type of main because no errors are returned from the function.
See also: