前言
《Zig Notes》是 Billgo 在学习 Zig 语言过程中整理而成,初次接触 Zig 的用户可以按序号依次阅读,对于有经验的 Zig 开发者可按需阅读感兴趣的章节。Zig 目前正在紧锣密鼓地开发中,Zig 语言及其标准库都在不断发展,本指南以最新的 Zig 开发版本为目标,部分代码有可能编译不通过。如果你下载了最新版本的 Zig,但在运行某些代码时遇到问题,请提 Issue (opens in a new tab)。
什么是 Zig ?
Zig 是一种通用的编程语言和工具链,用于维护健壮、最优和可重用的软件
为何是 Zig ?
从本质上看,Zig 是一门 low level 的高级语言,它和 C 很像,但改善旧问题并提供了完善的工具链,并且它可选支持 libc。
Zig 特性
- 类型是一等公民
- 无隐式执行
- 手动内存控制,精确的内存泄漏跟踪
- 完善的堆栈跟踪(在裸机器上也可以)
- 对交叉编译的一流支持
- 标准库集成 libc ,但不依赖它
- 可选类型代替 null
- 编译期(可以简单看作在编译时执行的函数)执行实现泛型和反射
- 无需 FFI/bindings 的 C 库集成
- 强大的构建系统
Zig 哲学
- Communicate intent precisely.
- Edge cases matter.
- Favor reading code over writing code.
- Only one obvious way to do things.
- Runtime crashes are better than bugs.
- Compile errors are better than runtime crashes.
- Incremental improvements.
- Avoid local maximums.
- Reduce the amount one must remember.
- Focus on code rather than style.
- Resource allocation may fail; resource deallocation must succeed.
- Memory is a resource.
- Together we serve the users.