brain

tamnd's digital brain — notes, problems, research

43815 notes

Appendix G. Common Compiler Errors

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...

zigbook
Appendix F. C Interop Reference

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.

zigbook
Appendix E. Build System Reference

Zig builds programs with Zig code. The build script is named:

zigbook
Appendix D. Standard Library Map

The Zig standard library is imported as:

zigbook
Appendix C. Builtin Functions

Builtin functions are part of the language. Their names begin with @.

zigbook
Appendix B. Operators and Precedence

This appendix lists the common operators by use. When an expression is not obvious, use parentheses.

zigbook
Appendix A. Zig Syntax Summary

This appendix summarizes the core syntax of Zig 0.16. It is a compact reference, not a tutorial.

zigbook
Appendix A. Zig Syntax Summary

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...

zigbook
Where to Go From Here

The programs in this chapter are small, but they have the shape of larger Zig programs.

zigbook
A Parallel File Scanner

This section combines the ideas from the previous sections into one program structure.

zigbook
demo

A program is finished only when another machine can build and run it reliably.

zigbook
Exercises

Exercise 19-1. Write a program that overflows a u8 using +=. Run it in Debug mode and observe the panic.

zigbook
Designing Concurrent Programs

Threads are a mechanism, not a design.

zigbook
Benchmarking

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.

zigbook
FFI Safety

FFI means foreign function interface. It is the boundary where Zig calls code written in another language, or where another language calls Zig.

zigbook
Semaphores

A semaphore is a counter used for synchronization.

zigbook
Exercises

This section collects the chapter exercises into one working set.

zigbook
Exercises

Exercise 16-1. Write a C file containing this function:

zigbook
Testing Strategy

Tests should be close to the code they check. Zig makes this easy with test blocks.

zigbook
Packed Memory

A normal struct is laid out for efficient access.

zigbook
Thread Pools

Creating a thread is expensive.

zigbook
Release Modes

Zig can build the same source code in different optimization modes.

zigbook
ABI and Layout

A C program and a Zig program can share data only when both sides agree on layout.

zigbook
Exercises

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.

zigbook
Cross Builds

Zig can build for a different target than the machine running the compiler.

zigbook
Dependencies

A Zig package may depend on another package.

zigbook
Examples

Examples are small programs kept inside the project.

zigbook
Exercises

This section collects the exercises for Chapter 14.

zigbook
Testing Utilities

Zig has built-in support for tests.

zigbook
Process APIs

The standard library gives access to process information through std.process.

zigbook
Exercises

This section collects the exercises for the chapter.

zigbook
Zig 0.16 I/O Changes

Zig 0.16 changes how I/O code is written in the standard library.

zigbook
Filesystem APIs

The standard library gives access to files and directories through std.fs.

zigbook
Error Handling in I/O

Input and output can fail.

zigbook
A Static Library

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...

zigbook
Volatile Memory

Most memory in a program behaves normally.

zigbook
Async Status in Zig 0.16

Zig once had async functions as a language feature.

zigbook
Static and Dynamic Linking

A program is made from object code.

zigbook
Calling Zig from C

C can call Zig when the Zig function is exported with a C-compatible ABI.

zigbook
Tests

Zig has tests in the language.

zigbook
Formatting

Zig uses format strings to turn values into text.

zigbook
Standard Input and Output

A program usually has three standard streams:

zigbook
Exercises

The exercises in this section are meant to make allocation habits precise. Each one should be written as a complete program unless stated otherwise.

zigbook
Ownership Rules

Allocation creates a responsibility.

zigbook
Fixed-Buffer Allocator

A fixed-buffer allocator uses memory that already exists.

zigbook
Arena Allocator

An arena allocator is used when many allocations have the same lifetime.

zigbook
Integer Overflow

An integer type can store only a fixed range of values.

zigbook
A Tiny HTTP Client

An HTTP client opens a network connection, sends a request, receives a response, and writes the response body.

zigbook
Condition Variables

A mutex protects shared data.

zigbook
libc and Freestanding Builds

A Zig target may use a C library, or it may use none.

zigbook
Linking Libraries

Calling a C function is only half the job. The linker must also find the code for that function.

zigbook
Build Options

A build option is a value passed to build.zig from the command line.

zigbook
Hash Maps

A hash map stores key-value pairs.

zigbook
Buffered I/O

Reading or writing one byte at a time is expensive.

zigbook
General-Purpose Allocator

The general-purpose allocator is used for ordinary heap allocation.

zigbook
Exercises

Exercise 11-1. Write a generic min function for values that support <.

zigbook
Compile-Time Dispatch

Generic functions in Zig are specialized at compile time.

zigbook
Interfaces by Convention

Zig has no built-in interface keyword.

zigbook
A Line Filter

A line filter reads text, changes or selects some lines, and writes the result. Many Unix programs have this shape.

zigbook
Pointer Casts

A pointer has a type.

zigbook
Atomics

A mutex makes one thread wait while another thread uses shared data.

zigbook
CPU Architectures

The architecture part of a target tells Zig what kind of processor the program will run on.

zigbook
Header Translation

@cImport does two jobs.

zigbook
Build Steps

A build file describes steps.

zigbook
`std.ArrayList`

std.ArrayList is a growable array.

zigbook
Writing Bytes

Writing bytes is the opposite of reading them.

zigbook
Page Allocator

The page allocator gets memory from the operating system.

zigbook
Type Constraints by Use

Zig does not have a formal trait or interface system.

zigbook
Exercises

1. Write a program that computes several Fibonacci numbers with comptime. Print the results at runtime.

zigbook
A File Copier

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.

zigbook
Safety Checks

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...

zigbook
Mutexes

A mutex is a lock for shared data.

zigbook
Operating Systems

The operating system part of a target tells Zig what kind of system the program will run on.

zigbook
`@cImport`

Writing every C declaration by hand is tedious. Zig can read C headers directly.

zigbook
`build.zig`

The file build.zig is a Zig program.

zigbook
`std.mem`

The std.mem module contains operations on memory, slices, bytes, and basic data movement. Much of Zig programming eventually passes through std.mem.

zigbook
Reading Bytes

A file is read as bytes.

zigbook
The Allocator Interface

An allocator is a value that knows how to allocate and free memory.

zigbook
Generic Structs

A generic struct is a function that returns a type.

zigbook
Generating Declarations

A Zig function can return a type.

zigbook
A Command-Line Parser

Many programs begin the same way: they read command-line arguments, decide what the user requested, then execute an operation.

zigbook
Undefined Behavior

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...

zigbook
Reflection

Reflection means inspecting a type from inside the program.

zigbook
Threads

A thread is an independent flow of execution.

zigbook
Target Triples

One of Zig's main design goals is cross compilation.

zigbook
Inline Loops

A normal loop runs while the program runs.

zigbook
Calling C

One of Zig's design goals is direct interoperability with C.

zigbook
`zig build`

Large programs are rarely built with a single compiler command.

zigbook
Types as Values

In Zig, a type is a value.

zigbook
Exercises

Exercise 9-17. Declare an optional integer.

zigbook
`std.debug`

The std.debug module contains utilities for debugging programs. The most commonly used function is print, which writes formatted output to standard error.

zigbook
Optionals Versus Errors

Optionals and errors both describe a value that may not be produced.

zigbook
Files

Most programs spend their time moving bytes.

zigbook
Compile-Time Parameters

A function parameter can be marked comptime.

zigbook
Optional Pointers

Pointers are often optional.

zigbook
Why Allocation Is Explicit

A program uses memory to store values.

zigbook
Generic Functions

A function in Zig can take types as parameters.

zigbook
Unwrapping Optionals

An optional value cannot be used as the payload type until it is unwrapped.

zigbook
What `comptime` Means

A Zig program runs in two stages.

zigbook
Null

null is the value used when an optional has no payload.

zigbook