brain
tamnd's digital brain — notes, problems, research
43815 notes
Zig tries to report errors at the point where the program becomes invalid. Many messages are direct: a value has the wrong type, a variable is unused, an error was ignored, or...
One of Zig's central goals is direct interoperability with C. Zig can call C code, compile C code, link system libraries, export functions to C, and translate C headers.
Zig builds programs with Zig code. The build script is named:
The Zig standard library is imported as:
Builtin functions are part of the language. Their names begin with @.
This appendix lists the common operators by use. When an expression is not obvious, use parentheses.
This appendix summarizes the core syntax of Zig 0.16. It is a compact reference, not a tutorial.
This appendix is a quick map of Zig syntax. It is not a grammar. The full Zig grammar is part of the official language reference. Zig 0.16 also keeps the language small enough...
The programs in this chapter are small, but they have the shape of larger Zig programs.
This section combines the ideas from the previous sections into one program structure.
A program is finished only when another machine can build and run it reliably.
Exercise 19-1. Write a program that overflows a u8 using +=. Run it in Debug mode and observe the panic.
Threads are a mechanism, not a design.
A benchmark measures the cost of a program or operation. The result is useful only if the measurement is repeatable and the work being measured is clearly defined.
FFI means foreign function interface. It is the boundary where Zig calls code written in another language, or where another language calls Zig.
A semaphore is a counter used for synchronization.
This section collects the chapter exercises into one working set.
Exercise 16-1. Write a C file containing this function:
Tests should be close to the code they check. Zig makes this easy with test blocks.
A normal struct is laid out for efficient access.
Creating a thread is expensive.
Zig can build the same source code in different optimization modes.
A C program and a Zig program can share data only when both sides agree on layout.
These exercises use the material from Chapters 15.1 through 15.8. Most are small. The goal is to practice reading and writing build.zig files until the structure becomes familiar.
Zig can build for a different target than the machine running the compiler.
A Zig package may depend on another package.
Examples are small programs kept inside the project.
This section collects the exercises for Chapter 14.
Zig has built-in support for tests.
The standard library gives access to process information through std.process.
This section collects the exercises for the chapter.
Zig 0.16 changes how I/O code is written in the standard library.
The standard library gives access to files and directories through std.fs.
Input and output can fail.
A library is code meant to be used by another program. In Zig, a library can be built from the same kind of source files as an executable. The difference is in how the build...
Most memory in a program behaves normally.
Zig once had async functions as a language feature.
A program is made from object code.
C can call Zig when the Zig function is exported with a C-compatible ABI.
Zig has tests in the language.
Zig uses format strings to turn values into text.
A program usually has three standard streams:
The exercises in this section are meant to make allocation habits precise. Each one should be written as a complete program unless stated otherwise.
Allocation creates a responsibility.
A fixed-buffer allocator uses memory that already exists.
An arena allocator is used when many allocations have the same lifetime.
An integer type can store only a fixed range of values.
An HTTP client opens a network connection, sends a request, receives a response, and writes the response body.
A mutex protects shared data.
A Zig target may use a C library, or it may use none.
Calling a C function is only half the job. The linker must also find the code for that function.
A build option is a value passed to build.zig from the command line.
A hash map stores key-value pairs.
Reading or writing one byte at a time is expensive.
The general-purpose allocator is used for ordinary heap allocation.
Exercise 11-1. Write a generic min function for values that support <.
Generic functions in Zig are specialized at compile time.
Zig has no built-in interface keyword.
A line filter reads text, changes or selects some lines, and writes the result. Many Unix programs have this shape.
A pointer has a type.
A mutex makes one thread wait while another thread uses shared data.
The architecture part of a target tells Zig what kind of processor the program will run on.
@cImport does two jobs.
A build file describes steps.
std.ArrayList is a growable array.
Writing bytes is the opposite of reading them.
The page allocator gets memory from the operating system.
Zig does not have a formal trait or interface system.
1. Write a program that computes several Fibonacci numbers with comptime. Print the results at runtime.
A file copier is a useful small program. It opens one file for reading, opens another file for writing, then copies bytes from the first to the second.
Zig inserts safety checks for operations that are valid only under certain conditions. These checks are present in safe build modes. They catch mistakes at the point where the...
A mutex is a lock for shared data.
The operating system part of a target tells Zig what kind of system the program will run on.
Writing every C declaration by hand is tedious. Zig can read C headers directly.
The file build.zig is a Zig program.
The std.mem module contains operations on memory, slices, bytes, and basic data movement. Much of Zig programming eventually passes through std.mem.
A file is read as bytes.
An allocator is a value that knows how to allocate and free memory.
A generic struct is a function that returns a type.
A Zig function can return a type.
Many programs begin the same way: they read command-line arguments, decide what the user requested, then execute an operation.
Zig gives the programmer direct access to memory, integers, pointers, and machine operations. This makes many programs simple and efficient. It also makes some operations...
Reflection means inspecting a type from inside the program.
A thread is an independent flow of execution.
One of Zig's main design goals is cross compilation.
A normal loop runs while the program runs.
One of Zig's design goals is direct interoperability with C.
Large programs are rarely built with a single compiler command.
In Zig, a type is a value.
Exercise 9-17. Declare an optional integer.
The std.debug module contains utilities for debugging programs. The most commonly used function is print, which writes formatted output to standard error.
Optionals and errors both describe a value that may not be produced.
Most programs spend their time moving bytes.
A function parameter can be marked comptime.
Pointers are often optional.
A program uses memory to store values.
A function in Zig can take types as parameters.
An optional value cannot be used as the payload type until it is unwrapped.
A Zig program runs in two stages.
null is the value used when an optional has no payload.