brain
tamnd's digital brain — notes, problems, research
42734 notes
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.
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'.
Sometimes a value may or may not exist.
Most functions should not decide what an error means.
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.
catch handles an error union.
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...
try is a shortcut for a common operation:
You now know the core ideas behind Zig.
Zig has not reached 1.0 yet. The current release line discussed in this book is Zig 0.16, not Zig 1.16.
Git stores snapshots of files.
IR means intermediate representation.
A function returns an error the same way it returns a normal value: with return.
Zig changes quickly because it is still moving toward 1.0.
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.
A game engine is a program structure for running interactive simulations.
An error union combines a normal value with an error set.
Programs fail for many reasons. A file may not exist. Memory allocation may fail. Input may be malformed. A network connection may close unexpectedly.
These exercises close the chapter on structs, enums, and unions. They are meant to make the data model concrete.
Performance ideas become clearer when you see them inside real code.
High performance concurrent design means using several threads or tasks without making the program slower, more fragile, or harder to reason about.
A struct is not only a list of fields. It also has a layout in memory.
A shell is a program that reads commands and runs other programs.
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.
Wrapping a C library means building a Zig layer around it.
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.
A union is a value that may hold one of several types.
An enum is a type whose values come from a fixed set of names.
Zig has no class syntax.
Fields are selected with the dot operator.
A struct is a type made from named fields.
This section closes the chapter. The exercises are small programs. Write them by hand. Compile them. Change them. Run them again.
Zig strings are bytes. UTF-8 is one way to interpret those bytes as text.
A Zig string is a sequence of bytes.
Some arrays have a special value after the last ordinary element. This value is called a sentinel.
A bytecode VM is a small machine inside your program.
Packaging means preparing your program so other people can download it, install it, run it, and trust what they are running.
A self-hosted compiler is a compiler written in the language it compiles.
An abstraction is a way to hide detail behind a simpler interface.
A CLI tool is a command-line program.
A queue is a data structure for passing work from one part of a program to another.
C headers are the bridge between Zig and C.
Benchmarking measures how fast code runs.
A slice is a view into an array.
An array literal creates an array value.
An array is a sequence of values of the same type.
1. Write a function increment that takes i32 and adds 1 to the pointed-to value.
A pointer is useful only while the value it points to still exists.
Memory has addresses. Types also have alignment.
Zig changes quickly before 1.0. That includes the build system.
Pointer arithmetic means forming a new pointer by moving from one element to another.
Packaging means preparing your program so another person can download it, install it, and run it.
A build does not only compile files. It can also install the results into a predictable output directory.
A slice is a pointer and a length.
A many-item pointer points to the first item in a sequence.
A single-item pointer points to one value.
A variable has a value. In Zig, a variable may also have an address.
1. Write a function max3 that returns the largest of three integers.
A program grows gradually.
Large programs are divided into smaller files. Zig uses @import to include declarations from another file.
Logging means recording what a program is doing.
Networking means communicating with another program through a network.
A Zig program may be split across many files. Declarations can be made visible outside a file with pub.
An HTTP client is code that sends a request to a web server and reads the response.
When a function is called, values are passed from the caller to the function parameters.
Functions communicate through parameters and return values.
JSON is a text format for storing structured data.
A function groups statements into a single operation. Functions are the basic unit of organization in a Zig program.
Compression means making data smaller.
This section collects the exercises for the chapter. They are meant to be small programs. Each one should compile and run.
defer runs a statement when control leaves the current block.
A process is a running program.
break leaves a loop.
A for loop visits the elements of an array, slice, or range.
An environment variable is a named value provided to a program by the operating system.
A while loop repeats while a condition is true.
A switch chooses one branch from several alternatives.
In Zig, if is an expression. It can produce a value.
Random numbers are used when a program needs variation.
A block is a sequence of statements inside braces.
This chapter introduced the basic forms of values and declarations: names, constants, variables, integer types, floating-point types, booleans, bytes, inferred types, and...
A data structure is never just a container.