brain
tamnd's digital brain — notes, problems, research
42734 notes
Zig does not perform most numeric conversions automatically.
A declaration may name its type explicitly.
A collection is a type that stores many values.
This chapter covered the builtins you will see most often as a beginner.
@embedFile reads a file at compile time and embeds its bytes into the final program.
Zig does not have a separate character type for ordinary strings.
A boolean value is either true or false.
Zig has floating-point types for numbers with fractional parts.
Zig has signed and unsigned integer types.
Zig has two kinds of variable declarations:
A Zig program is made from declarations.
The programs in this chapter are small. They are meant to be changed, broken, rebuilt, and studied.
A useful program reads text and counts something.
Programs often need input.
A program can compute a value before it prints it.
The function std.debug.print writes text.
A program works with values. In Zig, values are usually stored in constants or variables.
There are two common ways to run a small Zig program.
The first program in Zig is small.
The first program in Zig is small.
@compileError stops compilation with a custom error message.
@panic stops the program immediately with a message.
Zig code should be explicit, simple, and easy to inspect. The goal is not cleverness. The goal is code that another programmer can read, verify, and maintain.
Contributing to Zig means helping the language, compiler, standard library, documentation, tests, or tooling improve.
A lexer is the first stage of many programming language tools.
A cross-platform Zig program should not pretend that every operating system behaves the same way. Windows, Linux, macOS, WebAssembly, and embedded targets have different...
A compiler translates one form of code into another form.
Benchmarking means measuring how fast code runs.
A TCP server is a program that waits for clients to connect.
An event loop is code that waits for events, then runs the right piece of work for each event.
ABI means Application Binary Interface.
Memory debugging means finding mistakes in how a program uses memory.
Zig has built-in support for tests.
Programs often need to work with time.
@floatCast converts one floating-point value to another floating-point type.
This appendix lists useful Zig projects to read after you know the basics.
A programming language is never only its syntax.
A database stores data so it can be saved, searched, updated, and loaded again later.
A portable API is an interface that works across more than one platform.
A virtual machine is a program that runs another program.
Branch prediction is a CPU optimization.
A network protocol is a rulebook for how programs talk over a network.
await means: wait until an asynchronous operation has finished, then continue with its result.
Zig can call C, but C can also call Zig.
GDB and LLDB are debuggers.
Zig can build the same program in different optimization modes.
Parsing means turning text into data.
A tree is a collection made of nodes.
An allocation-friendly API makes memory behavior clear to the caller.
@truncate converts an integer to a smaller integer type by keeping only the low bits.
Zig is designed to work closely with C. You can call C from Zig, call Zig from C, compile C code with Zig, and link Zig programs against existing C libraries.
LLVM is a compiler infrastructure project.
A memory allocator is code that gives memory to the rest of a program.
Cross-target debugging means debugging a program built for a different machine, operating system, or CPU architecture than the one you are sitting at.
A parser reads text and turns it into structure.
Copying data is sometimes necessary, but unnecessary copying is one of the easiest ways to waste time and memory.
Endianness means the order used to store the bytes of a multi-byte value.
Async code lets a program start an operation now and receive the result later.
A mixed Zig and C project contains source files from both languages.
An assertion is a check that must be true while the program runs.
Linking is the step where the compiler connects your program with the code it depends on.
Formatting means turning values into text.
A ring buffer is a fixed-size queue that reuses its storage.
Memory lifetime means:
@intCast converts one integer value to another integer type.
comptime is one of Zig’s strongest features, but it should not be used everywhere.
When Zig compiles a program, it can build the program in different modes.
Code generation is the compiler stage that turns analyzed program meaning into target code.
A thread pool is a group of worker threads that wait for jobs.
ARM is a CPU architecture family used in phones, tablets, laptops, routers, Raspberry Pi boards, microcontrollers, servers, and many embedded devices. When you write Zig for...
A game engine core is the small central layer that runs the game.
Allocations are one of the most common causes of slow programs.
A binary file format stores data as bytes with a specific structure.
A condition variable lets one thread sleep until another thread says that something has changed.
Importing a C header lets Zig understand a C API. Linking gives the final program the actual compiled code.
A stack trace shows how your program reached a failure.
Cross compilation means building a program for a different machine than the one you are using.
A file lives inside a directory.
A bit set is a compact collection of yes-or-no values.
A memory leak happens when a program allocates memory and then loses the ability to free it.
@memcpy copies bytes from one memory region to another.
Metaprogramming means writing code that helps create, inspect, or specialize other code.
Most memory in Zig is ordinary memory.
A clear explanation of finding the longest string that divides both strings using the GCD of their lengths.
A clear explanation of minimizing the total score of triangulating a polygon using interval dynamic programming.
A clear explanation of minimizing total rounding error when rounding prices to meet a target sum using a greedy approach.
A clear explanation of determining if a source can reach a target in a very large grid with blocked cells using BFS with a cell count limit.
A clear explanation of counting land cells unreachable from the grid border using BFS from boundary land cells.
A clear explanation of computing total quantity sold per product using GROUP BY and SUM aggregation.
A clear explanation of finding the most experienced employee(s) for each project using a window function or correlated subquery.
A clear explanation of determining if a car can transport all passengers using a difference array for passenger count tracking.
A clear explanation of finding two non-overlapping subarrays with maximum combined sum using prefix sums and running maximums.
A clear explanation of computing the clumsy factorial by simulating cyclic operators with a stack.
A clear explanation of validating a string by repeatedly removing 'abc' substrings using a stack.
A clear explanation of minimizing total travel cost for two-city scheduling using a greedy refund approach after sending everyone to city A.
A clear explanation of pruning tree nodes where all root-to-leaf paths through them have sum less than a limit, using post-order DFS.
A clear explanation of finding all words that follow a two-word sequence in a text string.
A clear explanation of counting the occurrences of a specific digit in all numbers from 1 to n using digit dynamic programming.
A clear explanation of counting substrings of length k with all unique characters using a sliding window.
A clear explanation of finding the longest duplicate substring using binary search on length combined with Rabin-Karp rolling hash.