如果你想要一個可選的數值,那 Optional 是一個好選擇。
類似於 Rust 的 Option<T>,Zig 的 Optional 提供數值被賦予 null 的能力,以代表空、沒有。
基本
在宣告時個型別前加上 ? 使其變成可選類型。
const std = @import("std");
pub fn main() void { var value: ?u8 = null; std.debug.print("Value: {?}\n", .{value});
if (value == null) { value = 32; } std.debug.print("Value: {?}", .{value});}Value: nullValue: 32orelse
可以使用 orelse 來展開(unwrap)可選型別,將其變成一般的子型別。
const std = @import("std");
pub fn main() void { var a: ?u8 = null; const b: u8 = 32; var c = a orelse b; std.debug.print("Value: {}\n", .{c});
a = 128; c = a orelse b; std.debug.print("Value: {}\n", .{c});
std.debug.print("Type: {}", .{@TypeOf(c)});}Value: 32Value: 128Type: u8unreachable
如果你確定該可選值目前不是 null,可以使用 .? 直接展開成子型別的數值。如果對 null 進行 .? 會在執行期引發 Painc 錯誤。
value.? 和 value orelse unreachable 具有相同的效果。
const std = @import("std");
pub fn main() void { var a: ?u8 = 32; var c = a.?; std.debug.print("Value: {}\n", .{c});
a = null; c = a.?; // painc! std.debug.print("Value: {}\n", .{c});}Value: 32thread 55352 panic: attempt to use null value參考
本文以 Zig 0.13.0 為主。並同時發佈在: