学习

示例

按学习顺序排列的可运行示例及可复制的命令。

示例

示例目录是学习 Zero 的最佳实践路径。每个示例都小巧、确定性,且可从仓库根目录直接检查或构建。

从这里开始:

bin/zero check examples/hello.0
bin/zero build --emit exe --target linux-musl-x64 examples/add.0 --out .zero/out/add
./.zero/out/add

微型配置快速验证:

bin/zero build --release tiny --target linux-musl-x64 examples/hello.0 --out .zero/out/hello-tiny
bin/zero size --json --release tiny --target linux-musl-x64 --out .zero/out/hello-tiny examples/hello.0

当前编译器可以为 hello world 生成一个 tiny hosted/musl 风格的产物。这并非完整的 no-libc 运行时。

构建配置:

bin/zero build --json --profile debug --target linux-musl-x64 examples/hello.0 --out .zero/out/hello-debug
bin/zero build --json --profile fast --target linux-musl-x64 examples/hello.0 --out .zero/out/hello-fast
bin/zero build --json --profile small --target linux-musl-x64 examples/hello.0 --out .zero/out/hello-small
bin/zero size --json --profile tiny --target linux-musl-x64 examples/fixed-vec.0

构建 JSON 报告包含 profileSemanticsprofileBudget。尺寸 JSON 额外包含 sizeBreakdownretentionReasonsoptimizationHints

直接辅助函数审计:

bin/zero size --json --target linux-musl-x64 examples/fixed-vec.0

直接的尺寸和图报告展示了为固定容量形状保留了哪些辅助函数。它们还使 vtable、方法注册表、泛型注册表、隐藏的分配器机制和反射的缺失变得可见。

交叉编译快速验证:

bin/zero build --release tiny --target linux-musl-x64 examples/hello.0 --out .zero/out/hello-linux-musl
bin/zero build --release tiny --target win32-x64.exe examples/hello.0 --out .zero/out/hello-win32
bin/zero size --json --release tiny --target linux-musl-x64 --out .zero/out/hello-linux-musl examples/hello.0

在 macOS 主机上,这些命令通过直接发射器生成 Linux 和 Windows 风格的输出产物。zero size --json 提供目标报告和产物大小,无需执行外部二进制文件。

核心示例:

  • examples/hello.0World、stdout、check!
  • examples/point.0:类型、字面量、字段和辅助函数。
  • examples/result-choice.0:枚举、选择、载荷绑定和 match
  • examples/fixed-vec.0:字段默认值、静态值参数、构造器风格方法和接收者调用。
  • examples/fallibility.0:命名错误和显式错误集。
  • examples/memory-primitives.0:span、固定缓冲区、引用、Maybe<T> 和分配器词汇。
  • examples/allocator-collections.0NullAlloc、固定缓冲区分配、Vec、空 map/set 元数据以及 zero mem --json 分配器预算报告。
  • examples/compile-time-v1.0:有界 meta、目标/类型反射事实、Bool 和枚举静态值,以及编译时 JSON 元数据。
  • examples/ownership-cleanup.0owned<T> 清理、规范 drop 和词法作用域退出时的 defer
  • examples/std-path-io.0:固定缓冲区 std.path 辅助函数和调用者拥有的 std.io 缓冲区。
  • examples/std-data-formats.0std.codec 编码器和基本的显式分配器 std.json 辅助函数。
  • examples/std-json-bytes.0:字节 span JSON 验证、解析和令牌流。
  • examples/std-http-json.0:托管 HTTP 请求信封到调用者拥有的存储,随后进行字节 span JSON 解析。
  • examples/std-http-request.0:带有自定义方法、头部和请求体的托管 HTTP 请求信封。
  • examples/std-http-headers.0:托管 HTTP 请求信封、响应缓冲区和头部值查找。
  • examples/std-platform.0std.timestd.randstd.procstd.crypto 能力型辅助函数。
  • examples/cli-file.0:参数、环境变量、文件写入、stdout 和 stderr。
  • examples/file-copy.0Fsowned<File>、读/写资源能力和自动关闭。
  • examples/zero-hash/:带参数回退的文件校验和 CLI、固定缓冲区、托管读取和字节级 CRC-32。
  • examples/readall-cli/:固定缓冲区分配器使用和拥有的字节缓冲区读取。
  • examples/resource-cli/:包本地模块、资源清理和托管文件系统能力使用。
  • examples/memory-package/:无托管文件系统依赖的目标中立包辅助函数检查。
  • examples/error-tour/:错误示例、解释和常见诊断的规范修复。
  • examples/agent-repair-demo/:一个脚本化的代理循环,检查 JSON 诊断、解释代码、规划修复、应用编辑并重新运行检查。

使用 examples/README.md 中的索引查看完整的学习顺序和可复制的命令。

原生工作流覆盖:

  • 参数和环境变量:examples/cli-file.0 读取 std.argsstd.env
  • 文件系统资源:examples/zero-hash/ 通过显式能力进行种子设定和读取托管文件。
  • 确定性退出状态:examples/direct-exe-return.0 构建为返回 42 的 tiny 直接原生可执行文件。
  • 未处理错误退出路径:examples/direct-unhandled-error-exit.0 保持可失败退出路径可见。

更大的 CLI:zero-hash

examples/zero-hash/ 是一个较大的 CLI 示例。它涵盖了:

  • 参数和托管文件系统访问
  • 固定缓冲区分配
  • 内存辅助函数
  • 编解码/哈希风格 API
  • 可失败性和拥有的资源

它在没有大型标准库的情况下仍然保持实用。

构建命令:

bin/zero build --emit exe --target linux-musl-x64 examples/zero-hash --out .zero/out/zero-hash

运行命令:

pnpm run native:test

预期输出:

zero-hash ok

尺寸输出:

bin/zero size --json --target linux-musl-x64 examples/zero-hash --out .zero/out/zero-hash-size.json

检查元数据:

bin/zero graph --json examples/zero-hash

图和尺寸报告展示了 zero-hash 背后的辅助函数使用:参数、固定缓冲区分配、CRC-32 字节和托管读取。它们还表明该程序不保留隐藏的运行时机制,如全局分配、注册表、反射或堆分配。

基准测试用例:

ZERO_BENCH_RUNS=1 pnpm run bench

基准测试报告包含仅限 Zero 的 zero-hash 用例,并带有预期输出检查。

交叉目标状态:

bin/zero check --json --target linux-musl-x64 examples/zero-hash

zero-hash 有意使用托管文件系统 API。请使用 examples/memory-package/ 进行避免托管文件系统需求的目标中立直接构建。

使用 examples/memory-package/ 进行目标中立的跨目标直接构建。