3 Allocators, data, and comptime
This chapter covers
- Interacting with files in a cross-platform manner
- Thinking critically about memory management with Allocators
- Making code reliable with errors and error sets
- Loops, defer, and error-based control flow mechanisms
- Using the Zig build system and comptime to customize compiled programs
We’re about to write cat. Over and over again, actually. You know cat, right? It’s a standard and ubiquitous program for shell scripting, made for concatenating files and commonly used to print file contents to the console.
I can hear you groan through the pages of this book: "Cat is so simple and straightforward, isn’t it? How could such a program even contain enough content to deserve a chapter in this otherwise excellent Zig book?"
Well, I’m interested in providing a grounded view on Zig and perhaps systems programming in general. The programs that underpin the world’s infrastructure, the programs you use every day without even a moment of consideration, the programs you couldn’t live without — these programs don’t need to be complex. They don’t need state-of-the-art algorithms or millions of lines of code. Useful code doesn’t need to be glamorous, it just needs to do the job you need in the way you expect.