const std = @import("std");
const expect = std.testing.expect;
const eq = std.testing.expectEqual;
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
try stdout.print("Blue Sky | mbsky.com | sum={d}\n", .{add(3, 4)});
}
fn add(a: i32, b: i32) i32 { return a + b; }
const Point = struct { x: i32, y: i32 };
fn readName(name: []const u8) error{Missing}!usize {
if (name.len == 0) return error.Missing;
return name.len;
}
test "functions struct optional and explicit errors" {
try eq(@as(i32, 7), add(3, 4));
const p = Point{ .x = 10, .y = 20 };
try eq(@as(i32, 30), p.x + p.y);
const maybe: ?u32 = 8;
try eq(@as(u32, 8), maybe orelse 0);
try std.testing.expectError(error.Missing, readName(""));
try eq(@as(usize, 9), try readName("mbsky.com"));
}
test "escaped identifiers and keyword distinction" {
const @"two words" = 3;
const @"if" = 4;
try eq(@as(i32, 7), @"two words" + @"if");
try expect(std.zig.Token.keywords.get("fn") != null);
try expect(std.zig.Token.keywords.get("void") == null);
}
本例需在 Windows / Zig0.14.1 / Debug模式;0.15+及其它系统另验 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows / Zig0.14.1 / Debug模式;0.15+及其它系统另验
入口文件:projects/zig-lab/zig-basic-syntax.zig
预期结果与验证条件观察提示
- 该文件全部具名测试真实通过,未跳过或把编译成功当成执行完成。
- 具体输入、分支及数值由本章源码的expect/eq/expectError逐项核对;源码哈希与运行记录对应。
请结合实际环境逐项观察。
附件与关联工程包下载
课程关联工程包(登记关联14章) · 含当前章节或实例 · mbskycom202611a544.zip