brain

tamnd's digital brain — notes, problems, research

43815 notes

Exercises

1. Write a function parseUpper that accepts a byte and returns the uppercase letter value. Return error.InvalidUppercase if the byte is not between 'A' and 'Z'.

zigbook
The Optional Type

Sometimes a value may or may not exist.

zigbook
Designing Error Boundaries

Most functions should not decide what an error means.

zigbook
`defer` and `errdefer`

A program that opens a file must close it. A program that allocates memory must free it. A program that locks a mutex must unlock it.

zigbook
`catch`

catch handles an error union.

zigbook
Appendix A. Zig 1.16 New Features

As of the current public Zig release line, the version is Zig 0.16, not Zig 1.16. Zig has not reached 1.0 yet. In this appendix, read “Zig 1.16” as “Zig 0.16” unless...

zigbook
`try`

try is a shortcut for a common operation:

zigbook
Conclusion

You now know the core ideas behind Zig.

zigbook
Appendix A. Zig 1.16 New Features

Zig has not reached 1.0 yet. The current release line discussed in this book is Zig 0.16, not Zig 1.16.

zigbook
Build a Mini Git Clone

Git stores snapshots of files.

zigbook
Understanding Zig IR

IR means intermediate representation.

zigbook
Returning Errors

A function returns an error the same way it returns a normal value: with return.

zigbook
Following Zig Development

Zig changes quickly because it is still moving toward 1.0.

zigbook
Appendix J. Migrating Between Zig Versions

Zig is still before 1.0, so the language and standard library can change between releases. Code written for one Zig version may not compile on another version without edits.

zigbook
Build a Simple Game Engine

A game engine is a program structure for running interactive simulations.

zigbook
Error Unions

An error union combines a normal value with an error set.

zigbook
Error Sets

Programs fail for many reasons. A file may not exist. Memory allocation may fail. Input may be malformed. A network connection may close unexpectedly.

zigbook
Exercises

These exercises close the chapter on structs, enums, and unions. They are meant to make the data model concrete.

zigbook
Case Study 1: Reusing a Temporary Buffer

Performance ideas become clearer when you see them inside real code.

zigbook
High Performance Concurrent Design

High performance concurrent design means using several threads or tasks without making the program slower, more fragile, or harder to reason about.

zigbook
Data Layout

A struct is not only a list of fields. It also has a layout in memory.

zigbook
Writing a Small Shell

A shell is a program that reads commands and runs other programs.

zigbook
Data Races

A data race happens when two threads access the same memory at the same time, at least one access writes, and there is no proper synchronization.

zigbook
Wrapping Existing Libraries

Wrapping a C library means building a Zig layer around it.

zigbook
Packed Structs

A normal struct is laid out for ordinary program use. The compiler may add padding between fields so that each field has a suitable address.

zigbook
Tagged Unions

A union is a value that may hold one of several types.

zigbook
Enums

An enum is a type whose values come from a fixed set of names.

zigbook
Methods as Functions

Zig has no class syntax.

zigbook
Field Access

Fields are selected with the dot operator.

zigbook
Struct Declarations

A struct is a type made from named fields.

zigbook
Exercises

This section closes the chapter. The exercises are small programs. Write them by hand. Compile them. Change them. Run them again.

zigbook
UTF-8 Handling

Zig strings are bytes. UTF-8 is one way to interpret those bytes as text.

zigbook
Strings Are Bytes

A Zig string is a sequence of bytes.

zigbook
Sentinel Arrays

Some arrays have a special value after the last ordinary element. This value is called a sentinel.

zigbook
Build a Bytecode VM

A bytecode VM is a small machine inside your program.

zigbook
Packaging Cross-Platform Apps

Packaging means preparing your program so other people can download it, install it, run it, and trust what they are running.

zigbook
Self-Hosted Zig Compiler

A self-hosted compiler is a compiler written in the language it compiles.

zigbook
The Basic Idea

An abstraction is a way to hide detail behind a simpler interface.

zigbook
Writing a CLI Tool

A CLI tool is a command-line program.

zigbook
Channels and Queues

A queue is a data structure for passing work from one part of a program to another.

zigbook
Working with Headers

C headers are the bridge between Zig and C.

zigbook
Benchmarking Code

Benchmarking measures how fast code runs.

zigbook
Slicing Arrays

A slice is a view into an array.

zigbook
Array Literals

An array literal creates an array value.

zigbook
Fixed-Size Arrays

An array is a sequence of values of the same type.

zigbook
Exercises

1. Write a function increment that takes i32 and adds 1 to the pointed-to value.

zigbook
Lifetimes

A pointer is useful only while the value it points to still exists.

zigbook
Alignment

Memory has addresses. Types also have alignment.

zigbook
Build System Changes in Zig 0.16

Zig changes quickly before 1.0. That includes the build system.

zigbook
Pointer Arithmetic

Pointer arithmetic means forming a new pointer by moving from one element to another.

zigbook
Packaging Applications

Packaging means preparing your program so another person can download it, install it, and run it.

zigbook
Installing Binaries

A build does not only compile files. It can also install the results into a predictable output directory.

zigbook
Slices

A slice is a pointer and a length.

zigbook
Many-Item Pointers

A many-item pointer points to the first item in a sequence.

zigbook
Single-Item Pointers

A single-item pointer points to one value.

zigbook
Addresses and Pointer Types

A variable has a value. In Zig, a variable may also have an address.

zigbook
Exercises

1. Write a function max3 that returns the largest of three integers.

zigbook
Organizing a Small Program

A program grows gradually.

zigbook
Importing Files

Large programs are divided into smaller files. Zig uses @import to include declarations from another file.

zigbook
Logging

Logging means recording what a program is doing.

zigbook
Networking APIs

Networking means communicating with another program through a network.

zigbook
Public Declarations

A Zig program may be split across many files. Declarations can be made visible outside a file with pub.

zigbook
HTTP Client

An HTTP client is code that sends a request to a web server and reads the response.

zigbook
Passing Values

When a function is called, values are passed from the caller to the function parameters.

zigbook
Parameters and Return Values

Functions communicate through parameters and return values.

zigbook
JSON Support

JSON is a text format for storing structured data.

zigbook
Defining Functions

A function groups statements into a single operation. Functions are the basic unit of organization in a Zig program.

zigbook
Compression

Compression means making data smaller.

zigbook
Exercises

This section collects the exercises for the chapter. They are meant to be small programs. Each one should compile and run.

zigbook
`defer`

defer runs a statement when control leaves the current block.

zigbook
Process Management

A process is a running program.

zigbook
`break`, `continue`, and Labels

break leaves a loop.

zigbook
`for` Loops

A for loop visits the elements of an array, slice, or range.

zigbook
Environment Variables

An environment variable is a named value provided to a program by the operating system.

zigbook
`while` Loops

A while loop repeats while a condition is true.

zigbook
`switch` Expressions

A switch chooses one branch from several alternatives.

zigbook
`if` Expressions

In Zig, if is an expression. It can produce a value.

zigbook
Random Number Generation

Random numbers are used when a program needs variation.

zigbook
Blocks are Expressions

A block is a sequence of statements inside braces.

zigbook
Exercises

This chapter introduced the basic forms of values and declarations: names, constants, variables, integer types, floating-point types, booleans, bytes, inferred types, and...

zigbook
Start with the Operations

A data structure is never just a container.

zigbook
Explicit Casts

Zig does not perform most numeric conversions automatically.

zigbook
Type Inference

A declaration may name its type explicitly.

zigbook
Start with the Rule

A collection is a type that stores many values.

zigbook
Important Builtins in Zig 1.16

This chapter covered the builtins you will see most often as a beginner.

zigbook
`@embedFile`

@embedFile reads a file at compile time and embeds its bytes into the final program.

zigbook
Characters and Bytes

Zig does not have a separate character type for ordinary strings.

zigbook
Booleans

A boolean value is either true or false.

zigbook
Floating-Point Types

Zig has floating-point types for numbers with fractional parts.

zigbook
Integer Types

Zig has signed and unsigned integer types.

zigbook
`const` and `var`

Zig has two kinds of variable declarations:

zigbook
Names and Declarations

A Zig program is made from declarations.

zigbook
Exercises

The programs in this chapter are small. They are meant to be changed, broken, rebuilt, and studied.

zigbook
A Word-Count Program

A useful program reads text and counts something.

zigbook
Reading Command-Line Arguments

Programs often need input.

zigbook
A Small Calculator

A program can compute a value before it prints it.

zigbook
Printing Values

The function std.debug.print writes text.

zigbook
Variables and Constants

A program works with values. In Zig, values are usually stored in constants or variables.

zigbook
Build and Run

There are two common ways to run a small Zig program.

zigbook
Hello, Zig

The first program in Zig is small.

zigbook