zig-0.15
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseZig 0.15.x Programming Guide
Zig 0.15.x 编程指南
Version Scope: This skill is pinned to Zig 0.15.x (specifically 0.15.2). For master/nightly builds, APIs may differ. Always check official docs for your version.
This skill ensures correct Zig 0.15.x API usage. Many LLMs have outdated Zig knowledge (0.11-0.14), causing compilation errors.
Official Documentation (0.15.2):
- Language Reference: https://ziglang.org/documentation/0.15.2/
- Standard Library: https://ziglang.org/documentation/0.15.2/std/
- Release Notes: https://ziglang.org/download/0.15.1/release-notes.html
- Build System Guide: https://ziglang.org/learn/build-system/
Community Resources (0.15.x):
- Zig Cookbook: https://cookbook.ziglang.cc/ (practical recipes, tracks 0.15.x)
- Zig Cookbook Source: https://github.com/zigcc/zig-cookbook
Production Zig Codebases (learn from real-world projects):
- Bun: https://github.com/oven-sh/bun (JS runtime, ~200k+ lines of Zig)
- Tigerbeetle: https://github.com/tigerbeetle/tigerbeetle (financial database)
- Mach Engine: https://github.com/hexops/mach (game engine)
Learning Resources (All 0.15.x Compatible):
- Zig Algorithms: https://github.com/TheAlgorithms/Zig (data structures & algorithms)
- zig-clap: https://github.com/Hejsil/zig-clap (CLI argument parser, 1.4k stars)
- zig-bench: https://github.com/Hejsil/zig-bench (benchmarking library, 68 stars)
- libvaxis: https://github.com/rockorager/libvaxis (TUI framework, 1.5k stars, v0.15.2)
- ZLS: https://github.com/zigtools/zls (Language Server, 4.5k stars, v0.15.0)
- zig-protobuf: https://github.com/Arwalk/zig-protobuf (Protocol Buffers, 365 stars)
- libxev: https://github.com/mitchellh/libxev (event loop, 2k+ stars)
- zig-aio: https://github.com/Cloudef/zig-aio (async I/O library)
- zig-toml: https://github.com/sam701/zig-toml (TOML parser)
- zig-xml: https://github.com/nektro/zig-xml (XML parser)
- log.zig: https://github.com/karlseguin/log.zig (structured logging)
- http.zig: https://github.com/karlseguin/http.zig (HTTP server, 1.3k stars)
- cache.zig: https://github.com/karlseguin/cache.zig (thread-safe LRU cache, 79 stars)
- pretty: https://github.com/timfayz/pretty (pretty printer for debugging, 98 stars)
- tls.zig: https://github.com/ianic/tls.zig (TLS 1.2/1.3, client & server)
- zig-network: https://github.com/ikskuh/zig-network (TCP/UDP networking)
- zmath: https://github.com/zig-gamedev/zmath (SIMD math library)
Mitchell Hashimoto's Zig Libraries (Ghostty author, high quality):
- libxev: https://github.com/mitchellh/libxev (cross-platform event loop)
- zig-graph: https://github.com/mitchellh/zig-graph (directed graph data structure)
- zig-libxml2: https://github.com/mitchellh/zig-libxml2 (libxml2 bindings)
Compiler/Toolchain:
- llvm-zig: https://github.com/kassane/llvm-zig (LLVM/Clang bindings, 53 stars)
AI/ML:
- ZML: https://github.com/zml/zml (high-performance AI inference, 3k stars)
Blockchain/Ethereum:
- Zeam: https://github.com/blockblaz/zeam (Ethereum client in Zig)
- ssz.zig: https://github.com/blockblaz/ssz.zig (SSZ serialization for Eth2, 30 stars, ⚠️ 0.14.x)
Ecosystem Index:
- awesome-zig: https://github.com/zigcc/awesome-zig (curated list, ⚠️ check version compatibility!)
- Zigistry: https://zigistry.dev/ (package registry with version info)
For Other Versions:
- Master (unstable): https://ziglang.org/documentation/master/
- Source Code: https://codeberg.org/ziglang/zig
- All Releases: https://ziglang.org/download/
版本范围:本技能适配Zig 0.15.x版本(具体为0.15.2)。 对于master/nightly构建版本,API可能存在差异,请务必查阅对应版本的官方文档。
本技能确保你正确使用Zig 0.15.x的API。许多大语言模型对Zig的认知还停留在0.11-0.14版本,这会导致编译错误。
官方文档(0.15.2版本):
- 语言参考:https://ziglang.org/documentation/0.15.2/
- 标准库:https://ziglang.org/documentation/0.15.2/std/
- 发布说明:https://ziglang.org/download/0.15.1/release-notes.html
- 构建系统指南:https://ziglang.org/learn/build-system/
社区资源(0.15.x版本):
- Zig Cookbook:https://cookbook.ziglang.cc/(实用代码示例,适配0.15.x版本)
- Zig Cookbook源码:https://github.com/zigcc/zig-cookbook
生产级Zig代码库(从实际项目中学习):
- Bun:https://github.com/oven-sh/bun(JS运行时,包含20万+行Zig代码)
- Tigerbeetle:https://github.com/tigerbeetle/tigerbeetle(金融数据库)
- Mach Engine:https://github.com/hexops/mach(游戏引擎)
学习资源(全部适配0.15.x版本):
- Zig Algorithms:https://github.com/TheAlgorithms/Zig(数据结构与算法)
- zig-clap:https://github.com/Hejsil/zig-clap(CLI参数解析器,1.4k星)
- zig-bench:https://github.com/Hejsil/zig-bench(基准测试库,68星)
- libvaxis:https://github.com/rockorager/libvaxis(TUI框架,1.5k星,v0.15.2版本)
- ZLS:https://github.com/zigtools/zls(语言服务器,4.5k星,v0.15.0版本)
- zig-protobuf:https://github.com/Arwalk/zig-protobuf(Protocol Buffers,365星)
- libxev:https://github.com/mitchellh/libxev(事件循环,2k+星)
- zig-aio:https://github.com/Cloudef/zig-aio(异步I/O库)
- zig-toml:https://github.com/sam701/zig-toml(TOML解析器)
- zig-xml:https://github.com/nektro/zig-xml(XML解析器)
- log.zig:https://github.com/karlseguin/log.zig(结构化日志)
- http.zig:https://github.com/karlseguin/http.zig(HTTP服务器,1.3k星)
- cache.zig:https://github.com/karlseguin/cache.zig(线程安全LRU缓存,79星)
- pretty:https://github.com/timfayz/pretty(调试用美化打印工具,98星)
- tls.zig:https://github.com/ianic/tls.zig(TLS 1.2/1.3,支持客户端与服务器)
- zig-network:https://github.com/ikskuh/zig-network(TCP/UDP网络编程)
- zmath:https://github.com/zig-gamedev/zmath(SIMD数学库)
Mitchell Hashimoto的Zig库(Ghostty作者,高质量):
- libxev:https://github.com/mitchellh/libxev(跨平台事件循环)
- zig-graph:https://github.com/mitchellh/zig-graph(有向图数据结构)
- zig-libxml2:https://github.com/mitchellh/zig-libxml2(libxml2绑定)
编译器/工具链:
AI/ML领域:
区块链/以太坊领域:
- Zeam:https://github.com/blockblaz/zeam(用Zig编写的以太坊客户端)
- ssz.zig:https://github.com/blockblaz/ssz.zig(Eth2的SSZ序列化,30星,⚠️ 适配0.14.x版本)
生态系统索引:
- awesome-zig:https://github.com/zigcc/awesome-zig(精选列表,⚠️ 请检查版本兼容性!)
- Zigistry:https://zigistry.dev/(带版本信息的包注册表)
其他版本资源:
Critical API Changes in Zig 0.15
Zig 0.15中的关键API变更
ArrayList (BREAKING)
ArrayList(破坏性变更)
All mutating methods now require explicit parameter:
allocatorzig
// ❌ WRONG (0.13 and earlier)
var list = std.ArrayList(T).init(allocator);
try list.append(item);
try list.appendSlice(items);
_ = try list.addOne();
_ = try list.toOwnedSlice();
// ✅ CORRECT (0.15+)
var list = try std.ArrayList(T).initCapacity(allocator, 16);
defer list.deinit(allocator);
try list.append(allocator, item);
try list.appendSlice(allocator, items);
_ = try list.addOne(allocator);
_ = try list.toOwnedSlice(allocator);
// AssumeCapacity variants do NOT need allocator
list.appendAssumeCapacity(item);| Method | 0.13 | 0.15+ |
|---|---|---|
| | |
| | |
| | |
| | |
| | |
| | |
所有可变方法现在都需要显式传入参数:
allocatorzig
// ❌ 错误写法(0.13及更早版本)
var list = std.ArrayList(T).init(allocator);
try list.append(item);
try list.appendSlice(items);
_ = try list.addOne();
_ = try list.toOwnedSlice();
// ✅ 正确写法(0.15+版本)
var list = try std.ArrayList(T).initCapacity(allocator, 16);
defer list.deinit(allocator);
try list.append(allocator, item);
try list.appendSlice(allocator, items);
_ = try list.addOne(allocator);
_ = try list.toOwnedSlice(allocator);
// AssumeCapacity系列方法不需要传入allocator
list.appendAssumeCapacity(item);| 方法 | 0.13版本写法 | 0.15+版本写法 |
|---|---|---|
| | |
| | |
| | |
| | |
| | |
| | |
HashMap
HashMap
Managed (stores allocator internally):
zig
var map = std.StringHashMap(V).init(allocator);
defer map.deinit();
try map.put(key, value); // No allocator neededUnmanaged (requires allocator for each operation):
zig
var map = std.StringHashMapUnmanaged(V){};
defer map.deinit(allocator);
try map.put(allocator, key, value); // Allocator requiredManaged版本(内部存储allocator):
zig
var map = std.StringHashMap(V).init(allocator);
defer map.deinit();
try map.put(key, value); // 不需要传入allocatorUnmanaged版本(每次操作都需要传入allocator):
zig
var map = std.StringHashMapUnmanaged(V){};
defer map.deinit(allocator);
try map.put(allocator, key, value); // 必须传入allocatorWritergate: New std.Io.Writer and std.Io.Reader (MAJOR REWRITE)
Writergate:全新的std.Io.Writer和std.Io.Reader(重大重写)
Zig 0.15 introduces completely new I/O interfaces. All old readers/writers are deprecated.
std.ioZig 0.15引入了完全全新的I/O接口。所有旧的读写器均已被弃用。
std.ioKey Changes
主要变更
- Non-generic: New interfaces are concrete types, not
anytype - Buffer in interface: User provides buffer, implementation decides minimum size
- Ring buffer based: More efficient peek and streaming operations
- Precise error sets: No more propagation
anyerror
- 非泛型:新接口为具体类型,而非
anytype - 接口内置缓冲区:由用户提供缓冲区,实现层决定最小尺寸
- 基于环形缓冲区:更高效的peek和流式操作
- 精确的错误集合:不再传播
anyerror
New stdout Pattern
新的stdout使用方式
zig
// ❌ WRONG (0.14 and earlier)
const stdout = std.io.getStdOut().writer();
try stdout.print("Hello\n", .{});
// ✅ CORRECT (0.15+)
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
const stdout: *std.Io.Writer = &stdout_writer.interface;
try stdout.print("Hello\n", .{});
try stdout.flush(); // Don't forget to flush!zig
// ❌ 错误写法(0.14及更早版本)
const stdout = std.io.getStdOut().writer();
try stdout.print("Hello\n", .{});
// ✅ 正确写法(0.15+版本)
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
const stdout: *std.Io.Writer = &stdout_writer.interface;
try stdout.print("Hello\n", .{});
try stdout.flush(); // 不要忘记刷新!Adapter for Migration
迁移适配工具
If you have old-style writers and need new interface:
zig
fn useOldWriter(old_writer: anytype) !void {
var adapter = old_writer.adaptToNewApi(&.{});
const w: *std.Io.Writer = &adapter.new_interface;
try w.print("{s}", .{"example"});
}如果你有旧风格的写入器,需要适配新接口:
zig
fn useOldWriter(old_writer: anytype) !void {
var adapter = old_writer.adaptToNewApi(&.{});
const w: *std.Io.Writer = &adapter.new_interface;
try w.print("{s}", .{"example"});
}New Reader API
新的Reader API
zig
// Reading lines with delimiter
while (reader.takeDelimiterExclusive('\n')) |line| {
// process line
} else |err| switch (err) {
error.EndOfStream => {},
error.StreamTooLong => return err,
error.ReadFailed => return err,
}zig
// 按分隔符读取行
while (reader.takeDelimiterExclusive('\n')) |line| {
// 处理行内容
} else |err| switch (err) {
error.EndOfStream => {},
error.StreamTooLong => return err,
error.ReadFailed => return err,
}HTTP Client (MAJOR REWRITE)
HTTP客户端(重大重写)
Zig 0.15.2 has both (high-level) and (low-level) APIs:
fetch()request()zig
// ✅ CORRECT: High-level fetch() API (Zig 0.15.2)
var client: std.http.Client = .{ .allocator = allocator };
defer client.deinit();
const result = try client.fetch(.{
.location = .{ .url = "https://example.com/api" },
.method = .GET,
});
// result.status contains the HTTP status
// ✅ CORRECT: Low-level request/response API (Zig 0.15.2)
var client: std.http.Client = .{ .allocator = allocator };
defer client.deinit();
const uri = try std.Uri.parse("https://example.com/api");
// Create request with client.request() (NOT client.open!)
var req = try client.request(.POST, uri, .{
.extra_headers = &.{
.{ .name = "Content-Type", .value = "application/json" },
},
});
defer req.deinit();
// Send body (use @constCast for const slices)
try req.sendBodyComplete(@constCast("{\"key\": \"value\"}"));
// Receive response
var redirect_buf: [4096]u8 = undefined;
var response = try req.receiveHead(&redirect_buf);
// Check status
const status = @intFromEnum(response.head.status);
if (status >= 200 and status < 300) {
// Read response body using std.Io.Reader
var body_reader = response.reader(&redirect_buf);
const body = try body_reader.allocRemaining(allocator, std.Io.Limit.limited(1024 * 1024));
defer allocator.free(body);
}Key API functions (verified in Zig 0.15.2):
- →
client.request(method, uri, options)Request - →
client.fetch(options)FetchResult - - sends body and flushes
req.sendBodyComplete(body) - - for GET requests without body
req.sendBodiless() - →
req.receiveHead(buffer)Response - →
response.reader(buffer)*std.Io.Reader - →
reader.allocRemaining(allocator, limit)[]u8
Zig 0.15.2同时提供(高层级)和(低层级)API:
fetch()request()zig
// ✅ 正确写法:高层级fetch() API(Zig 0.15.2)
var client: std.http.Client = .{ .allocator = allocator };
defer client.deinit();
const result = try client.fetch(.{
.location = .{ .url = "https://example.com/api" },
.method = .GET,
});
// result.status包含HTTP状态码
// ✅ 正确写法:低层级request/response API(Zig 0.15.2)
var client: std.http.Client = .{ .allocator = allocator };
defer client.deinit();
const uri = try std.Uri.parse("https://example.com/api");
// 使用client.request()创建请求(不要用client.open!)
var req = try client.request(.POST, uri, .{
.extra_headers = &.{
.{ .name = "Content-Type", .value = "application/json" },
},
});
defer req.deinit();
// 发送请求体(对const切片使用@constCast)
try req.sendBodyComplete(@constCast("{\"key\": \"value\"}"));
// 接收响应
var redirect_buf: [4096]u8 = undefined;
var response = try req.receiveHead(&redirect_buf);
// 检查状态码
const status = @intFromEnum(response.head.status);
if (status >= 200 and status < 300) {
// 使用std.Io.Reader读取响应体
var body_reader = response.reader(&redirect_buf);
const body = try body_reader.allocRemaining(allocator, std.Io.Limit.limited(1024 * 1024));
defer allocator.free(body);
}已验证的Zig 0.15.2关键API函数:
- →
client.request(method, uri, options)Request - →
client.fetch(options)FetchResult - - 发送请求体并刷新
req.sendBodyComplete(body) - - 用于无请求体的GET请求
req.sendBodiless() - →
req.receiveHead(buffer)Response - →
response.reader(buffer)*std.Io.Reader - →
reader.allocRemaining(allocator, limit)[]u8
Base64 Encoding
Base64编码
zig
// ❌ WRONG (0.13)
const encoder = std.base64.standard;
const encoded = encoder.encode(&buf, data);
// ✅ CORRECT (0.15+)
const encoder = std.base64.standard.Encoder;
const encoded = encoder.encode(&buf, data);zig
// ❌ 错误写法(0.13版本)
const encoder = std.base64.standard;
const encoded = encoder.encode(&buf, data);
// ✅ 正确写法(0.15+版本)
const encoder = std.base64.standard.Encoder;
const encoded = encoder.encode(&buf, data);Ed25519 Cryptography (MAJOR CHANGES)
Ed25519加密(重大变更)
Key types are now structs, not raw byte arrays:
zig
const Ed25519 = std.crypto.sign.Ed25519;
// ❌ WRONG (0.13 - SecretKey was [32]u8)
const secret: [32]u8 = ...;
const kp = Ed25519.KeyPair.fromSecretKey(secret);
// ✅ CORRECT (0.15+ - SecretKey is struct with 64 bytes)
// From 32-byte seed (deterministic):
const seed: [32]u8 = ...;
const kp = try Ed25519.KeyPair.generateDeterministic(seed);
// From 64-byte secret key:
var secret_bytes: [64]u8 = ...;
const secret_key = try Ed25519.SecretKey.fromBytes(secret_bytes);
const kp = try Ed25519.KeyPair.fromSecretKey(secret_key);
// Get public key bytes:
const pubkey_bytes: [32]u8 = kp.public_key.toBytes();
// Get seed:
const seed: [32]u8 = kp.secret_key.seed();Signature changes:
zig
// ❌ WRONG (0.13 - fromBytes returned error union)
const sig = try Ed25519.Signature.fromBytes(bytes);
// ✅ CORRECT (0.15+ - fromBytes does NOT return error)
const sig = Ed25519.Signature.fromBytes(bytes);密钥类型现在为结构体,而非原始字节数组:
zig
const Ed25519 = std.crypto.sign.Ed25519;
// ❌ 错误写法(0.13版本 - SecretKey为[32]u8)
const secret: [32]u8 = ...;
const kp = Ed25519.KeyPair.fromSecretKey(secret);
// ✅ 正确写法(0.15+版本 - SecretKey为包含64字节的结构体)
// 从32字节种子生成(确定性):
const seed: [32]u8 = ...;
const kp = try Ed25519.KeyPair.generateDeterministic(seed);
// 从64字节密钥生成:
var secret_bytes: [64]u8 = ...;
const secret_key = try Ed25519.SecretKey.fromBytes(secret_bytes);
const kp = try Ed25519.KeyPair.fromSecretKey(secret_key);
// 获取公钥字节:
const pubkey_bytes: [32]u8 = kp.public_key.toBytes();
// 获取种子:
const seed: [32]u8 = kp.secret_key.seed();签名相关变更:
zig
// ❌ 错误写法(0.13版本 - fromBytes返回错误联合类型)
const sig = try Ed25519.Signature.fromBytes(bytes);
// ✅ 正确写法(0.15+版本 - fromBytes不返回错误)
const sig = Ed25519.Signature.fromBytes(bytes);Type Introspection Enums (Case Change)
类型自省枚举(大小写变更)
zig
// ❌ WRONG (0.13 - PascalCase)
if (@typeInfo(T) == .Slice) { ... }
if (@typeInfo(T) == .Pointer) { ... }
if (@typeInfo(T) == .Struct) { ... }
// ✅ CORRECT (0.15+ - lowercase/escaped)
if (@typeInfo(T) == .slice) { ... }
if (@typeInfo(T) == .pointer) { ... }
if (@typeInfo(T) == .@"struct") { ... }
if (@typeInfo(T) == .@"enum") { ... }
if (@typeInfo(T) == .@"union") { ... }
if (@typeInfo(T) == .array) { ... }
if (@typeInfo(T) == .optional) { ... }zig
// ❌ 错误写法(0.13版本 - 大驼峰命名)
if (@typeInfo(T) == .Slice) { ... }
if (@typeInfo(T) == .Pointer) { ... }
if (@typeInfo(T) == .Struct) { ... }
// ✅ 正确写法(0.15+版本 - 小写/转义)
if (@typeInfo(T) == .slice) { ... }
if (@typeInfo(T) == .pointer) { ... }
if (@typeInfo(T) == .@"struct") { ... }
if (@typeInfo(T) == .@"enum") { ... }
if (@typeInfo(T) == .@"union") { ... }
if (@typeInfo(T) == .array) { ... }
if (@typeInfo(T) == .optional) { ... }Custom Format Functions
自定义格式化函数
zig
// ❌ WRONG (0.13 - used {} format specifier)
pub fn format(
self: Self,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
_ = fmt;
_ = options;
try writer.writeAll("...");
}
// Usage: std.fmt.bufPrint(&buf, "{}", .{value});
// ✅ CORRECT (0.15+ - use {f} format specifier)
pub fn format(self: Self, writer: anytype) !void {
_ = self;
try writer.writeAll("...");
}
// Usage: std.fmt.bufPrint(&buf, "{f}", .{value});zig
// ❌ 错误写法(0.13版本 - 使用{}格式说明符)
pub fn format(
self: Self,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
_ = fmt;
_ = options;
try writer.writeAll("...");
}
// 使用方式:std.fmt.bufPrint(&buf, "{}", .{value});
// ✅ 正确写法(0.15+版本 - 使用{f}格式说明符)
pub fn format(self: Self, writer: anytype) !void {
_ = self;
try writer.writeAll("...");
}
// 使用方式:std.fmt.bufPrint(&buf, "{f}", .{value});JSON Parsing and Serialization
JSON解析与序列化
zig
const MyStruct = struct {
name: []const u8,
value: u32,
};
// ✅ CORRECT: Parsing (0.15+)
const json_str =
\\{"name": "test", "value": 42}
;
const parsed = try std.json.parseFromSlice(MyStruct, allocator, json_str, .{});
defer parsed.deinit();
const data = parsed.value;
// ✅ CORRECT: Serialization (0.15.2 - writer-based API)
// Note: There is NO stringifyAlloc in 0.15.2!
// Method 1: Using std.json.Stringify with Allocating writer
var out: std.Io.Writer.Allocating = .init(allocator);
defer out.deinit();
var stringify: std.json.Stringify = .{
.writer = &out.writer,
.options = .{},
};
try stringify.write(data);
const json_output = out.written();
// Method 2: Using std.fmt with json.fmt wrapper
const formatted = try std.fmt.allocPrint(allocator, "{f}", .{std.json.fmt(data, .{})});
defer allocator.free(formatted);
// Method 3: To fixed buffer
var buf: [1024]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buf);
var writer = fbs.writer();
var stringify2: std.json.Stringify = .{
.writer = &writer.adaptToNewApi(&.{}).new_interface,
.options = .{},
};
try stringify2.write(data);
const output = fbs.getWritten();zig
const MyStruct = struct {
name: []const u8,
value: u32,
};
// ✅ 正确写法:解析(0.15+版本)
const json_str =
\\{"name": "test", "value": 42}
;
const parsed = try std.json.parseFromSlice(MyStruct, allocator, json_str, .{});
defer parsed.deinit();
const data = parsed.value;
// ✅ 正确写法:序列化(0.15.2 - 基于写入器的API)
// 注意:0.15.2版本中没有stringifyAlloc!
// 方法1:使用std.json.Stringify和Allocating写入器
var out: std.Io.Writer.Allocating = .init(allocator);
defer out.deinit();
var stringify: std.json.Stringify = .{
.writer = &out.writer,
.options = .{},
};
try stringify.write(data);
const json_output = out.written();
// 方法2:使用std.fmt配合json.fmt包装器
const formatted = try std.fmt.allocPrint(allocator, "{f}", .{std.json.fmt(data, .{})});
defer allocator.free(formatted);
// 方法3:写入固定缓冲区
var buf: [1024]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buf);
var writer = fbs.writer();
var stringify2: std.json.Stringify = .{
.writer = &writer.adaptToNewApi(&.{}).new_interface,
.options = .{},
};
try stringify2.write(data);
const output = fbs.getWritten();Memory/Formatting
内存/格式化
zig
// Allocating format
const formatted = try std.fmt.allocPrint(allocator, "value: {d}", .{42});
defer allocator.free(formatted);
// Non-allocating format
var buffer: [256]u8 = undefined;
const result = try std.fmt.bufPrint(&buffer, "value: {d}", .{42});zig
// 分配式格式化
const formatted = try std.fmt.allocPrint(allocator, "value: {d}", .{42});
defer allocator.free(formatted);
// 非分配式格式化
var buffer: [256]u8 = undefined;
const result = try std.fmt.bufPrint(&buffer, "value: {d}", .{42});Common Error Messages and Fixes
常见错误信息及修复方案
| Error | Cause | Fix |
|---|---|---|
| ArrayList method missing allocator | Add allocator as first argument |
| Using | Use |
| Using old HTTP API | Use |
| Ed25519 SecretKey is now struct | Use |
| Signature.fromBytes doesn't return error | Remove |
| @typeInfo enum case changed | Use lowercase |
| Old build.zig API | Use |
| 错误信息 | 原因 | 修复方案 |
|---|---|---|
| ArrayList方法缺少allocator参数 | 添加allocator作为第一个参数 |
| 使用了 | 使用 |
| 使用了旧的HTTP API | 使用 |
| Ed25519的SecretKey现在是结构体 | 使用 |
| Signature.fromBytes不再返回错误 | 移除 |
| @typeInfo枚举的成员名称已变更 | 使用小写的 |
| 使用了旧的build.zig API | 使用 |
Verification Workflow
验证流程
After writing Zig code:
- Run to check for compilation errors
zig build - If errors match patterns above, apply the 0.15 fix
- Run to verify functionality
zig build test - Use to catch UB
zig build -Doptimize=ReleaseFast test
编写完Zig代码后:
- 运行检查编译错误
zig build - 如果错误匹配上述模式,应用0.15版本的修复方案
- 运行验证功能
zig build test - 使用捕获未定义行为(UB)
zig build -Doptimize=ReleaseFast test
Build System (build.zig) Changes (MAJOR REWRITE)
构建系统(build.zig)变更(重大重写)
Official Guide: https://ziglang.org/learn/build-system/
Executable Creation
可执行文件创建
zig
// ❌ WRONG (0.14 and earlier)
const exe = b.addExecutable(.{
.name = "hello",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
// ✅ CORRECT (0.15+)
const exe = b.addExecutable(.{
.name = "hello",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
});zig
// ❌ 错误写法(0.14及更早版本)
const exe = b.addExecutable(.{
.name = "hello",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
// ✅ 正确写法(0.15+版本)
const exe = b.addExecutable(.{
.name = "hello",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
});Library Creation
库创建
zig
// ❌ WRONG (0.14 - addSharedLibrary/addStaticLibrary)
const lib = b.addSharedLibrary(.{
.name = "mylib",
.root_source_file = .{ .path = "src/lib.zig" },
});
// ✅ CORRECT (0.15+ - unified addLibrary with linkage)
const lib = b.addLibrary(.{
.name = "mylib",
.linkage = .dynamic, // or .static
.root_module = b.createModule(.{
.root_source_file = b.path("src/lib.zig"),
.target = target,
.optimize = optimize,
}),
});zig
// ❌ 错误写法(0.14版本 - 使用addSharedLibrary/addStaticLibrary)
const lib = b.addSharedLibrary(.{
.name = "mylib",
.root_source_file = .{ .path = "src/lib.zig" },
});
// ✅ 正确写法(0.15+版本 - 使用统一的addLibrary并指定linkage)
const lib = b.addLibrary(.{
.name = "mylib",
.linkage = .dynamic, // 或.static
.root_module = b.createModule(.{
.root_source_file = b.path("src/lib.zig"),
.target = target,
.optimize = optimize,
}),
});Path Handling
路径处理
zig
// ❌ WRONG (0.14 - .path field)
.root_source_file = .{ .path = "src/main.zig" },
// ✅ CORRECT (0.15+ - use b.path())
.root_source_file = b.path("src/main.zig"),zig
// ❌ 错误写法(0.14版本 - 使用.path字段)
.root_source_file = .{ .path = "src/main.zig" },
// ✅ 正确写法(0.15+版本 - 使用b.path())
.root_source_file = b.path("src/main.zig"),Module Dependencies
模块依赖
zig
// ❌ WRONG (0.14)
exe.addModule("sdk", sdk_module);
// ✅ CORRECT (0.15+)
exe.root_module.addImport("sdk", sdk_module);zig
// ❌ 错误写法(0.14版本)
exe.addModule("sdk", sdk_module);
// ✅ 正确写法(0.15+版本)
exe.root_module.addImport("sdk", sdk_module);Target Options
目标选项
zig
// ❌ WRONG (0.14)
const target = b.standardTargetOptions(.{});
// then use target directly
// ✅ CORRECT (0.15+)
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "app",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target, // pass to createModule
.optimize = optimize, // pass to createModule
}),
});zig
// ❌ 错误写法(0.14版本)
const target = b.standardTargetOptions(.{});
// 直接使用target
// ✅ 正确写法(0.15+版本)
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "app",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target, // 传递给createModule
.optimize = optimize, // 传递给createModule
}),
});Testing
测试
zig
// ✅ CORRECT (0.15+)
const unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = b.graph.host, // use b.graph.host for native target
}),
});
const run_unit_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);zig
// ✅ 正确写法(0.15+版本)
const unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = b.graph.host, // 使用b.graph.host作为原生目标
}),
});
const run_unit_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);Complete build.zig Example
完整的build.zig示例
zig
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "hello",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
});
b.installArtifact(exe);
const run_exe = b.addRunArtifact(exe);
const run_step = b.step("run", "Run the application");
run_step.dependOn(&run_exe.step);
}zig
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "hello",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
});
b.installArtifact(exe);
const run_exe = b.addRunArtifact(exe);
const run_step = b.step("run", "Run the application");
run_step.dependOn(&run_exe.step);
}std.mem Patterns
std.mem使用模式
Memory Operations
内存操作
zig
// Copy memory
@memcpy(dest, src); // Same as std.mem.copyForwards
// Set memory
@memset(buffer, 0); // Zero-fill
// Compare memory
const equal = std.mem.eql(u8, slice1, slice2);
// Find in slice
const index = std.mem.indexOf(u8, haystack, needle);zig
// 复制内存
@memcpy(dest, src); // 与std.mem.copyForwards功能相同
// 设置内存
@memset(buffer, 0); // 零填充
// 比较内存
const equal = std.mem.eql(u8, slice1, slice2);
// 在切片中查找
const index = std.mem.indexOf(u8, haystack, needle);Alignment
对齐
zig
// Check alignment
const is_aligned = std.mem.isAligned(@intFromPtr(ptr), @alignOf(T));
// Align pointer
const aligned = std.mem.alignPointer(ptr, @alignOf(T));zig
// 检查对齐
const is_aligned = std.mem.isAligned(@intFromPtr(ptr), @alignOf(T));
// 对齐指针
const aligned = std.mem.alignPointer(ptr, @alignOf(T));std.io Patterns
std.io使用模式
Fixed Buffer Stream
固定缓冲区流
zig
var buf: [1024]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buf);
const writer = fbs.writer();
try writer.writeAll("Hello");
try writer.print(" {d}", .{42});
const written = fbs.getWritten(); // "Hello 42"zig
var buf: [1024]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buf);
const writer = fbs.writer();
try writer.writeAll("Hello");
try writer.print(" {d}", .{42});
const written = fbs.getWritten(); // "Hello 42"Counting Writer
计数写入器
zig
var counting = std.io.countingWriter(underlying_writer);
try counting.writer().writeAll("test");
const bytes_written = counting.bytes_written; // 4zig
var counting = std.io.countingWriter(underlying_writer);
try counting.writer().writeAll("test");
const bytes_written = counting.bytes_written; // 4Testing Patterns
测试模式
Testing Allocator (Detects Leaks)
测试分配器(检测内存泄漏)
zig
test "no memory leaks" {
const allocator = std.testing.allocator; // Auto-detects leaks
const data = try allocator.alloc(u8, 100);
defer allocator.free(data); // MUST free or test fails
// ... test code ...
}zig
test "no memory leaks" {
const allocator = std.testing.allocator; // 自动检测泄漏
const data = try allocator.alloc(u8, 100);
defer allocator.free(data); // 必须释放,否则测试失败
// ... 测试代码 ...
}Assertions
断言
zig
test "assertions" {
try std.testing.expect(condition); // Boolean
try std.testing.expectEqual(expected, actual); // Equality
try std.testing.expectEqualSlices(u8, expected, actual); // Slices
try std.testing.expectError(error.SomeError, result); // Error
try std.testing.expectEqualStrings("hello", str); // Strings
}zig
test "assertions" {
try std.testing.expect(condition); // 布尔断言
try std.testing.expectEqual(expected, actual); // 相等断言
try std.testing.expectEqualSlices(u8, expected, actual); // 切片相等断言
try std.testing.expectError(error.SomeError, result); // 错误断言
try std.testing.expectEqualStrings("hello", str); // 字符串相等断言
}Comptime Patterns
编译期模式
Type Reflection
类型反射
zig
fn serialize(comptime T: type, value: T) ![]u8 {
const info = @typeInfo(T);
switch (info) {
.int => |i| {
// Handle integer
const byte_count = @divExact(i.bits, 8);
// ...
},
.@"struct" => |s| {
// Handle struct
inline for (s.fields) |field| {
// Process each field
const field_value = @field(value, field.name);
// ...
}
},
else => @compileError("Unsupported type"),
}
}zig
fn serialize(comptime T: type, value: T) ![]u8 {
const info = @typeInfo(T);
switch (info) {
.int => |i| {
// 处理整数
const byte_count = @divExact(i.bits, 8);
// ...
},
.@"struct" => |s| {
// 处理结构体
inline for (s.fields) |field| {
// 处理每个字段
const field_value = @field(value, field.name);
// ...
}
},
else => @compileError("Unsupported type"),
}
}Optional Type Handling
可选类型处理
zig
fn getInner(comptime T: type) type {
const info = @typeInfo(T);
if (info == .optional) {
return info.optional.child;
}
return T;
}zig
fn getInner(comptime T: type) type {
const info = @typeInfo(T);
if (info == .optional) {
return info.optional.child;
}
return T;
}Pointer and Slice Patterns
指针与切片模式
Slice to Many-Item Pointer
切片转多元素指针
zig
const slice: []u8 = buffer[0..10];
const ptr: [*]u8 = slice.ptr;zig
const slice: []u8 = buffer[0..10];
const ptr: [*]u8 = slice.ptr;Creating Slices
创建切片
zig
// From array
const arr = [_]u8{ 1, 2, 3, 4, 5 };
const slice = arr[1..4]; // [2, 3, 4]
// From pointer + length
const slice = ptr[0..len];zig
// 从数组创建
const arr = [_]u8{ 1, 2, 3, 4, 5 };
const slice = arr[1..4]; // [2, 3, 4]
// 从指针+长度创建
const slice = ptr[0..len];Sentinel-Terminated Slices
哨兵终止切片
zig
// Null-terminated string
const str: [:0]const u8 = "hello";
const c_str: [*:0]const u8 = str.ptr;
// Get length without sentinel
const len = str.len; // 5, not including nullzig
// 空终止字符串
const str: [:0]const u8 = "hello";
const c_str: [*:0]const u8 = str.ptr;
// 获取不含哨兵的长度
const len = str.len; // 5,不包含空字符Language Changes
语言变更
usingnamespace Removed
usingnamespace已被移除
The keyword has been completely removed in Zig 0.15.
usingnamespacezig
// ❌ WRONG (removed in 0.15)
pub usingnamespace @import("other.zig");
// ✅ CORRECT - explicit re-exports
pub const foo = @import("other.zig").foo;
pub const bar = @import("other.zig").bar;
// ✅ CORRECT - namespace via field
pub const other = @import("other.zig");
// Usage: other.foo, other.barMigration for mixins: Use zero-bit fields with :
@fieldParentPtrzig
// ❌ OLD mixin pattern
pub const Foo = struct {
count: u32 = 0,
pub usingnamespace CounterMixin(Foo);
};
// ✅ NEW mixin pattern (0.15+)
pub fn CounterMixin(comptime T: type) type {
return struct {
pub fn increment(m: *@This()) void {
const x: *T = @alignCast(@fieldParentPtr("counter", m));
x.count += 1;
}
};
}
pub const Foo = struct {
count: u32 = 0,
counter: CounterMixin(Foo) = .{}, // zero-bit field
};
// Usage: foo.counter.increment()Zig 0.15中已完全移除关键字。
usingnamespacezig
// ❌ 错误写法(0.15版本已移除)
pub usingnamespace @import("other.zig");
// ✅ 正确写法 - 显式重导出
pub const foo = @import("other.zig").foo;
pub const bar = @import("other.zig").bar;
// ✅ 正确写法 - 通过字段命名空间
pub const other = @import("other.zig");
// 使用方式:other.foo, other.barMixin迁移方案:使用零位字段配合:
@fieldParentPtrzig
// ❌ 旧的mixin模式
pub const Foo = struct {
count: u32 = 0,
pub usingnamespace CounterMixin(Foo);
};
// ✅ 新的mixin模式(0.15+版本)
pub fn CounterMixin(comptime T: type) type {
return struct {
pub fn increment(m: *@This()) void {
const x: *T = @alignCast(@fieldParentPtr("counter", m));
x.count += 1;
}
};
}
pub const Foo = struct {
count: u32 = 0,
counter: CounterMixin(Foo) = .{}, // 零位字段
};
// 使用方式:foo.counter.increment()async/await Keywords Removed
async/await关键字已被移除
The , , and have been removed. Async functionality will be provided via the standard library's new I/O interface.
asyncawait@frameSizezig
// ❌ REMOVED - no async/await keywords
async fn fetchData() ![]u8 { ... }
const result = await fetchData();
// ✅ Use std.Io interfaces or threads insteadasyncawait@frameSizezig
// ❌ 已移除 - 不再支持async/await关键字
async fn fetchData() ![]u8 { ... }
const result = await fetchData();
// ✅ 使用std.Io接口或线程替代Arithmetic on undefined
对undefined值的算术操作
Operations on that could trigger illegal behavior now cause compile errors:
undefinedzig
const a: u32 = 0;
const b: u32 = undefined;
// ❌ COMPILE ERROR in 0.15+
_ = a + b; // error: use of undefined value here causes illegal behavior对可能触发非法行为的值进行操作现在会导致编译错误:
undefinedzig
const a: u32 = 0;
const b: u32 = undefined;
// ❌ 0.15+版本编译错误
_ = a + b; // 错误:此处使用undefined值会导致非法行为Lossy Integer to Float Coercion
整数到浮点数的有损转换
Compile error if integer cannot be precisely represented:
zig
// ❌ COMPILE ERROR in 0.15+
const val: f32 = 123_456_789; // error: cannot represent precisely
// ✅ CORRECT - opt-in to floating-point rounding
const val: f32 = 123_456_789.0;如果整数无法被精确表示为浮点数,将触发编译错误:
zig
// ❌ 0.15+版本编译错误
const val: f32 = 123_456_789; // 错误:无法精确表示
// ✅ 正确写法 - 选择启用浮点数舍入
const val: f32 = 123_456_789.0;Extended References (This Skill)
扩展参考(本技能)
详细的 API 参考和迁移指南请查阅以下文档:
| 文档 | 路径 | 内容 |
|---|---|---|
| 标准库 API 详解 | | ArrayList、HashMap、HTTP Client、Ed25519、Base64、JSON、@typeInfo、std.fmt 等完整 API 参考 |
| 迁移模式指南 | | 从 0.13/0.14 迁移到 0.15 的详细对照,包括 Writergate、ArrayList、Build System 等 |
| 生产级代码库 | | Sig(Solana)、ZML(AI)、Zeam(Ethereum)、Bun、Tigerbeetle 等项目学习指南,以及 0.15.x 兼容库列表 |
| 版本策略 | | 版本兼容性说明和更新策略 |
详细的 API 参考和迁移指南请查阅以下文档:
| 文档 | 路径 | 内容 |
|---|---|---|
| 标准库 API 详解 | | ArrayList、HashMap、HTTP Client、Ed25519、Base64、JSON、@typeInfo、std.fmt 等完整 API 参考 |
| 迁移模式指南 | | 从 0.13/0.14 迁移到 0.15 的详细对照,包括 Writergate、ArrayList、Build System 等 |
| 生产级代码库 | | Sig(Solana)、ZML(AI)、Zeam(Ethereum)、Bun、Tigerbeetle 等项目学习指南,以及 0.15.x 兼容库列表 |
| 版本策略 | | 版本兼容性说明和更新策略 |
快速查阅建议
快速查阅建议
- 编写 HTTP 请求? → 查看 的
references/stdlib-api-reference.md部分std.http.Client - ArrayList 报错? → 查看 的
references/migration-patterns.md部分ArrayList Migration - 学习最佳实践? → 查看 中的 Sig 项目(Solana 验证器)
references/production-codebases.md - 寻找第三方库? → 查看 的
references/production-codebases.md表格Smaller Learning Projects
- 编写 HTTP 请求? → 查看 的
references/stdlib-api-reference.md部分std.http.Client - ArrayList 报错? → 查看 的
references/migration-patterns.md部分ArrayList Migration - 学习最佳实践? → 查看 中的 Sig 项目(Solana 验证器)
references/production-codebases.md - 寻找第三方库? → 查看 的
references/production-codebases.md表格Smaller Learning Projects
References
参考资料
Official Documentation (0.15.2):
- Language Reference: https://ziglang.org/documentation/0.15.2/
- Standard Library Docs: https://ziglang.org/documentation/0.15.2/std/
- Release Notes (0.15.1): https://ziglang.org/download/0.15.1/release-notes.html
- Build System Guide: https://ziglang.org/learn/build-system/
Community Resources (0.15.x):
- Zig Cookbook: https://cookbook.ziglang.cc/
- Zig Cookbook Source: https://github.com/zigcc/zig-cookbook
Cookbook Recipe Categories (all tested on 0.15.x):
- File System: read files, mmap, iterate directories
- Cryptography: SHA-256, PBKDF2, Argon2
- Network: TCP/UDP client/server
- Web: HTTP GET/POST, HTTP server
- Concurrency: threads, shared data, thread pools
- Encoding: JSON, ZON, base64
- Database: SQLite, PostgreSQL, MySQL
Production Zig Codebases (learn from real-world projects):
| Project | URL | Learn From |
|---|---|---|
| Sig | https://github.com/Syndica/sig | Solana validator in Zig - most relevant for this SDK! |
| Zeam | https://github.com/blockblaz/zeam | Ethereum client in Zig - blockchain patterns |
| Bun | https://github.com/oven-sh/bun | JS runtime, async I/O, FFI, build system |
| Tigerbeetle | https://github.com/tigerbeetle/tigerbeetle | Financial DB, deterministic execution, testing |
| Mach | https://github.com/hexops/mach | Game engine, graphics, memory management |
| Ghostty | https://github.com/ghostty-org/ghostty | Terminal emulator, cross-platform, GPU rendering |
| Zig Algorithms | https://github.com/TheAlgorithms/Zig | Data structures, algorithms, idiomatic Zig |
Key Sections in Release Notes:
- Writergate (I/O rewrite): https://ziglang.org/download/0.15.1/release-notes.html#Writergate
- ArrayList changes: https://ziglang.org/download/0.15.1/release-notes.html#ArrayList-make-unmanaged-the-default
- usingnamespace removal: https://ziglang.org/download/0.15.1/release-notes.html#usingnamespace-Removed
- Format method changes: https://ziglang.org/download/0.15.1/release-notes.html#f-Required-to-Call-format-Methods
Source Code & Development:
- Official Repository: https://codeberg.org/ziglang/zig
- Master Documentation: https://ziglang.org/documentation/master/
- Master Std Library: https://ziglang.org/documentation/master/std/
官方文档(0.15.2版本):
- 语言参考:https://ziglang.org/documentation/0.15.2/
- 标准库文档:https://ziglang.org/documentation/0.15.2/std/
- 发布说明(0.15.1):https://ziglang.org/download/0.15.1/release-notes.html
- 构建系统指南:https://ziglang.org/learn/build-system/
社区资源(0.15.x版本):
- Zig Cookbook:https://cookbook.ziglang.cc/
- Zig Cookbook源码:https://github.com/zigcc/zig-cookbook
Cookbook示例分类(全部在0.15.x版本测试通过):
- 文件系统:读取文件、mmap、遍历目录
- 加密:SHA-256、PBKDF2、Argon2
- 网络:TCP/UDP客户端/服务器
- Web:HTTP GET/POST、HTTP服务器
- 并发:线程、共享数据、线程池
- 编码:JSON、ZON、base64
- 数据库:SQLite、PostgreSQL、MySQL
生产级Zig代码库(从实际项目中学习):
| 项目 | 链接 | 学习点 |
|---|---|---|
| Sig | https://github.com/Syndica/sig | 用Zig编写的Solana验证器 - 与本SDK最相关! |
| Zeam | https://github.com/blockblaz/zeam | 用Zig编写的以太坊客户端 - 区块链模式 |
| Bun | https://github.com/oven-sh/bun | JS运行时、异步I/O、FFI、构建系统 |
| Tigerbeetle | https://github.com/tigerbeetle/tigerbeetle | 金融数据库、确定性执行、测试 |
| Mach | https://github.com/hexops/mach | 游戏引擎、图形学、内存管理 |
| Ghostty | https://github.com/ghostty-org/ghostty | 终端模拟器、跨平台、GPU渲染 |
| Zig Algorithms | https://github.com/TheAlgorithms/Zig | 数据结构、算法、Zig惯用写法 |
发布说明中的关键章节:
- Writergate(I/O重写):https://ziglang.org/download/0.15.1/release-notes.html#Writergate
- ArrayList变更:https://ziglang.org/download/0.15.1/release-notes.html#ArrayList-make-unmanaged-the-default
- usingnamespace移除:https://ziglang.org/download/0.15.1/release-notes.html#usingnamespace-Removed
- 格式化方法变更:https://ziglang.org/download/0.15.1/release-notes.html#f-Required-to-Call-format-Methods
源码与开发:
Version Compatibility Notes
版本兼容性说明
This skill targets Zig 0.15.x. If you're using a different version:
| Version | Documentation | Notes |
|---|---|---|
| 0.15.x | This skill | Current stable, solana-zig uses 0.15.2 |
| 0.14.x | https://ziglang.org/documentation/0.14.1/ | Old build.zig API, old std.io |
| 0.13.x | https://ziglang.org/documentation/0.13.0/ | ArrayList without allocator param |
| master | https://ziglang.org/documentation/master/ | Unstable, APIs may change daily |
How to check your Zig version:
bash
zig version本技能针对Zig 0.15.x版本。如果你使用其他版本:
| 版本 | 文档 | 说明 |
|---|---|---|
| 0.15.x | 本技能 | 当前稳定版,solana-zig使用0.15.2版本 |
| 0.14.x | https://ziglang.org/documentation/0.14.1/ | 旧的build.zig API、旧的std.io |
| 0.13.x | https://ziglang.org/documentation/0.13.0/ | ArrayList不需要allocator参数 |
| master | https://ziglang.org/documentation/master/ | 不稳定版,API可能每日变更 |
如何检查你的Zig版本:
bash
zig versionor for this project:
或针对当前项目:
./solana-zig/zig version
**When APIs differ from this skill**:
1. Check your actual Zig version
2. Consult version-specific documentation
3. Use compiler errors as guidance - Zig has excellent error messages./solana-zig/zig version
**当API与本技能描述不符时**:
1. 检查你的实际Zig版本
2. 查阅对应版本的官方文档
3. 以编译器错误信息为指导 - Zig的错误信息非常完善