简介 (Introduction)
Zig 是一种通用的编程语言和工具链,旨在维护健壮、优化且可重用的软件。
- 健壮 (Robust):即使在内存不足等边缘情况下,行为也是正确的。
- 优化 (Optimal):意味着以程序能表现和运行的最佳方式编写代码。
- 可重用 (Reusable):代码适用于具有不同约束的多种环境。
- 可维护 (Maintainable):能够精确地向编译器和其他程序员传达意图,阅读开销低,并且对不断变化的需求和环境具有适应力。
本文档通过示例展示了如何使用 Zig 的各项功能,并且所有内容都在一个页面上,便于搜索。代码示例作为 Zig 主测试套件的一部分进行了编译和测试。HTML 文档是独立的,可以离线使用。
目录
- Hello World
- 注释 (Comments)
- 值 (Values)
- 字符串字面量 (String Literals)
- 赋值 (Assignment)
- 未定义 (undefined)
- 解构 (Destructuring)
- Zig 测试 (Zig Test)
- 整数 (Integers)
- 浮点数 (Floats)
- 运算符 (Operators)
- 数组 (Arrays)
- 向量 (Vectors)
- 指针 (Pointers)
- 切片 (Slices)
- 结构体 (struct)
- 枚举 (enum)
- 联合 (union)
- 不透明类型 (opaque)
- 代码块 (Blocks)
- Switch 语句 (switch)
- While 循环 (while)
- For 循环 (for)
- If 表达式 (if)
- 推迟执行 (defer)
- 不可达 (unreachable)
- 编译时基础 (Basics At Compile-Time)
- 不返回 (noreturn)
- 函数 (Functions)
- 错误处理 (Errors)
- 可选类型 (Optionals)
- 类型转换 (Casting)
- 零位类型 (Zero Bit Types)
- Void 与结果位置语义 (Void and Result Location Semantics)
- 编译时 (comptime)
- 汇编 (Assembly)
- 原子操作 (Atomics)
- 异步函数 (Async Functions)
- 内置函数 (Builtin Functions)
- 构建模式 (Build Mode)
- 非法行为 (Illegal Behavior)
- 默认操作与非法行为 (Default Operations & UB)
- 内存管理 (Memory Management)
- 编译与源文件 (Compilation & Source)
- 入口点与标准库选项 (Entry Point & Std Options)
- Panic 处理器 (Panic Handler)
- 构建系统 (Zig Build System)
- C 语言集成 (C)
- WebAssembly
- 编译目标 (Targets)
- 风格指南 (Style Guide)
- 文档注释指南 (Doc Comment Guidance)
- 源文件编码 (Source Encoding)
- 关键字参考 (Keyword Reference)
- 附录 (Appendix)
- Zig 标准库 (Zig Standard Library)
- 变量 (Variables)
- 递归 (Recursion)
- 生命周期与所有权 (Lifetime and Ownership)
- 命令行接口 (CLI)