返回排行榜

pedropark99/zig-book

Zigpedropark99.github.io/zig-book/

An open, technical and introductory book for the Zig programming language 📚📖

bookprogrammingprogramming-languagezigziglangcoursetutorialtutorial-course
Star 增长趋势
Star
2.7k
Forks
164
周增长
Issues
24
1k2k
2024年7月2025年3月2025年11月2026年7月
README

Introduction to Zig

Hey! This is the official repository for the book "Introduction to Zig: a project-based book", written by Pedro Duarte Faria. To know more about the book, checkout the About this book section below. You can read the current version of the book in your web browser: https://pedropark99.github.io/zig-book/.

This book is crafted using a modern, reproducible publishing stack, leveraging the power of:

Zig   Quarto   R   Knitr   Pandoc   Nix

The core book content is built using the Quarto publishing system in conjunction with a little bit of R code (zig_engine.R), that is responsible for calling the Zig compiler to compile and run the Zig code examples.

Support the project!

If you like this project, and you want to support it, you can buy a PDF, eBook or a physical copy of the book, either at Amazon, or at Leanpub:

Sending donations directly

You can also donate some amount directly to the author of the project via:

  • PayPal Donation.
  • Revolut.

These are good ways to support directly the author of the project, which helps to foster more contents like this, and it makes possible for the author to keep writing helpful tools and materials for the community.

PayPal

PayPal

Revolut

You can send money via Swift Payment with the following bank and Swift details:

Recipient: Pedro Duarte Faria
BIC/SWIFT Code: REVOSGS2
Account number: 6124512226
Name and address of the bank: Revolut Technologies Singapore Pte. Ltd, 6 Battery Road, Floor 6-01, 049909, Singapore, Singapore
Corresponding BIC: CHASGB2L

If you do have a Revolut account, you can scan the following QR code:

http://revolut.me/pedroduartefaria

About this book

This is an open (i.e., open-source), technical and introductory book for the Zig programming language, which is a new general purpose, and low-level programming language for building optimal and robust software.

Official repository of the book: https://github.com/pedropark99/zig-book.

This book is designed for both beginners and experienced developers. It explores the exciting world of Zig through small and simple projects (in a similar style to the famous "Python Crash Course" book from Eric Matthes). Some of these projects are: a Base64 encoder/decoder, a HTTP Server and an image filter.

As you work through the book, you will learn:

  • The syntax of the language, and how it compares to C, C++ and Rust.
  • Data structures, memory allocators, filesystem and I/O.
  • Optionals as a new paradigm to handle nullability.
  • How to test and debug a Zig application.
  • Errors as values, and how to handle them.
  • How to build C and Zig code with the build system that is embedded into the language.
  • Zig interoperability with C.
  • Parallelism with threads and SIMD.
  • And more.

How to build the book

First of all, you need to care about the dependencies of the project. This book is built using the following pieces of software:

  1. The Zig compiler, which is responsible for compiling most of the code examples exposed in the book.
  2. The R programming language, which provides some useful tools, specially knitr and rmarkdown, which are responsible for collecting the code examples exposed across the book, and sending them to the zig compiler to be compiled and executed, and also, collecting the results back to include them in the book.
  3. The Quarto publishing system, which is responsible for compiling the book, creating internal links, references, a chapters structure, the HTML content of the book. It does all of that by using the amazing Pandoc with some extra features implemented by Quarto.

So, installing these three pieces of software (Zig, R, Quarto) in your current machine is the first step to build the book. There are two strategies that you can take here:

  1. You can install these three pieces of software manually (you can find instructions on how to install each piece by clicking in the above hyperlinks).
  2. Or, you can use the Nix Flake declared in the flake.nix file to create a reproducible environment that already comes with these pieces installed.

Building the book manually

If you decide to install the dependencies of the book manually, then, follow the "install instructions" that are present in the above hyperlinks, and, after you installed the three pieces (Zig, R and Quarto), follow the next instructions specified here.

Install R packages

After you installed the three pieces of software listed above, you should run the dependencies.R R script, to install some R packages that are used across the book. Just run the command below in your terminal, and you should be fine.

OBS: If you are on Linux or MacOS, this command will probably take some time to run, because every single dependency gets built from source. In Windows, this usually doesn't take that long because pre-built binaries are usually available.

Rscript dependencies.R

Render the book

If you installed Quarto correctly in your computer , you should be able to build the book by simply executing the following command in the terminal.

quarto render

Building the book with Nix Flake

If you choose to use the Nix Flake to create an environment that comes with the necessary dependencies, then, you need to open a terminal, and run the nix develop command on the root of the project:

nix develop

With this command, Nix will download the necessary packages and libraries, and then create a new bash session that points to a new environment that contains these packages and libraries in it. Then, inside this new bash session that is created by Nix, all you need to do is to run the quarto render command, which will kickstart Quarto, and make it build the book for you:

quarto render

How the Zig compiler is found

Some R code (zig_engine.R) is used to collect the Zig code examples found across the book, and send them to the Zig compiler, so that they can be compiled and executed.

But in order to do that, this R code needs to find the Zig compiler installed in your machine. This search process is done in two stages. First, it uses the Sys.which() function to find the path to the Zig compiler in your computer, which is just a R interface to the which command line tool.

This is a fast and easy approach, but, it doesn't work in all situations, specially if your Zig compiler is not installed in a "standard location" in your computer. That is why, a second strategy is applied, which is to search through the PATH environment variable.

It gets the value of your PATH environment variable, and iterates through the directories listed in this variable, trying to find the Zig compiler in one of them. This approach is much slower than the first one, but is more garanteed to work.

License

Copyright © 2024 Pedro Duarte Faria. This book is licensed by the CC-BY 4.0 Creative Commons Attribution 4.0 International Public License.

Creative Commons License

相关仓库
getify/You-Dont-Know-JS

A book series (2 published editions) on the JS language.

Otherbook-seriesjavascript
amazon.com/dp/B085XXCJ7X
184.6k33.5k
krahets/hello-algo

《Hello 算法》:动画图解、一键运行的数据结构与算法教程。支持简中、繁中、English、日本語,提供 Python, Java, C++, C, C#, JS, Go, Swift, Rust, Ruby, Kotlin, TS, Dart 等代码实现

JavaMavenOtheralgorithmsdata-structures
hello-algo.com
128.7k15.3k
d2l-ai/d2l-zh

《动手学深度学习》:面向中文读者、能运行、可讨论。中英文版被70多个国家的500多所大学用于教学。

PythonPyPIApache License 2.0deep-learningbook
zh.d2l.ai
79.1k12.3k
dylanaraps/pure-bash-bible

📖 A collection of pure bash alternatives to external processes.

ShellMIT Licensebashshell
41.7k3.6k
denysdovhan/wtfjs

🤪 A list of funny and tricky JavaScript examples

JavaScriptnpmDo What The F*ck You Want To Public Licensejavascriptspecification
bit.ly/wtfjavascript
37.7k2.7k
unknwon/the-way-to-go_ZH_CN

《The Way to Go》中文译本,中文正式名《Go 入门指南》

GoGo Modulesgotutorial
35.1k8.5k
sunface/rust-course

什么?你敢放心的把后背交给 AI? 我赌你不敢,那就来学学 AI 时代最酷、最安全、最快的语言吧。本书拥有全面且深入的讲解、生动贴切的示例、德芙般丝滑的内容,这可能是目前最用心的 Rust 中文学习教程 / Book

Rustcrates.iorust-langrust
course.rs
30.7k2.6k
realpython/python-guide

Python best practices guidebook, written for humans.

BatchfileOtherpythonguide
docs.python-guide.org
29.7k5.9k
d2l-ai/d2l-en

Interactive deep learning book with multi-framework code, math, and discussions. Adopted at 500 universities from 70 countries including Stanford, MIT, Harvard, and Cambridge.

PythonPyPIOtherdeep-learningmachine-learning
d2l.ai
29.2k5.1k
HandsOnLLM/Hands-On-Large-Language-Models

Official code repo for the O'Reilly Book - "Hands-On Large Language Models"

Jupyter NotebookApache License 2.0artificial-intelligencebook
llm-book.com
27.7k6.4k
koodo-reader/koodo-reader

A modern ebook manager and reader with sync and backup capacities for Windows, macOS, Linux, Android, iOS and Web

JavaScriptnpmGNU Affero General Public License v3.0epubreader
koodoreader.com
27.6k2.1k
yeasy/docker_practice

最新Docker容器技术,从真实案例中学习最佳实践!| Learn and understand Docker&Container technologies, with real DevOps practice!

GoGo Modulesdockerbook
yeasy.gitbook.io/docker_practice/
26.2k5.8k