pub fn main() !void { // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
const stdout_file = std.io.getStdOut().writer(); var bw = std.io.bufferedWriter(stdout_file); const stdout = bw.writer(); try stdout.print("Run `zig build test` to run the tests.\n", .{});
try bw.flush(); // don't forget to flush! }
test "simple test" { var list = std.ArrayList(i32).init(std.testing.allocator); defer list.deinit(); // try commenting this out and see if zig detects the memory leak! try list.append(42); try std.testing.expectEqual(@as(i32, 42), list.pop()); }
建置:
1
zig build
執行:
1
zig build run
你應該會看到回應:
1 2
All your codebase are belong to us. Run `zig build test` to run the tests.
print
這邊要先特別介紹一下 print(),因為之後的範例會很常看到它。它用起來和 C 的差不多,每個 {} 都代表一個數值(就像 C 的 %d),後面的 .{ var },則是實際的變數或常數,如果有多個的話,要以前寫在 .{ } 內,並用逗號 , 分隔。如果沒有要填入數值的話 .{} 內留空。