跳转到内容
语言参考Zig 0.17.1官方原文

变量是内存存储的一个单元。

声明变量时通常优先使用 const 而非 var。这会减少人和计算机阅读代码时需要处理的信息,并创造更多优化机会。

变量绝不允许遮蔽外层作用域的标识符。

使用 extern 关键字或 @extern 内置函数,可以链接另一个目标文件导出的变量。使用 export 关键字或 @export 内置函数,可以让其他目标文件在链接时访问变量。两种情况下,变量类型都必须兼容 C ABI。

另见:

命名空间级变量

命名空间级变量具有全局生命周期,与声明顺序无关,并且按需求进行语义分析。它们的初始值隐式在编译时求值。如果命名空间级变量为 const,则其值在 comptime 已知,否则在运行时已知。

test_namespace_level_variables.zig
var y: i32 = add(10, x);
const x: i32 = add(12, 34);
test "container level variables" {
try expectEqual(46, x);
try expectEqual(56, y);
}
fn add(a: i32, b: i32) i32 {
return a + b;
}
const std = @import("std");
const expectEqual = std.testing.expectEqual;
命令行
$ zig test test_namespace_level_variables.zig
1/1 test_namespace_level_variables.test.container level variables...OK
All 1 tests passed.

命名空间级变量可以在 struct、union、enum 或 opaque 内声明:

test_namespaced_variable.zig
const std = @import("std");
const expectEqual = std.testing.expectEqual;
test "namespaced container level variable" {
try expectEqual(1235, foo());
try expectEqual(1236, foo());
}
const S = struct {
var x: i32 = 1234;
};
fn foo() i32 {
S.x += 1;
return S.x;
}
命令行
$ zig test test_namespaced_variable.zig
1/1 test_namespaced_variable.test.namespaced container level variable...OK
All 1 tests passed.

局部作用域中的全局变量

在函数内部使用命名空间,也可以创建具有全局生命周期的局部变量。

test_locally_scoped_global_variable.zig
const std = @import("std");
const expectEqual = std.testing.expectEqual;
test "static local variable" {
try expectEqual(1235, foo());
try expectEqual(1236, foo());
}
fn foo() i32 {
const S = struct {
var x: i32 = 1234;
};
S.x += 1;
return S.x;
}
命令行
$ zig test test_locally_scoped_global_variable.zig
1/1 test_locally_scoped_global_variable.test.static local variable...OK
All 1 tests passed.

线程局部变量

可以使用 threadlocal 关键字将变量指定为线程局部变量,使每个线程使用该变量的独立实例:

test_thread_local_variables.zig
const std = @import("std");
const assert = std.debug.assert;
threadlocal var x: i32 = 1234;
test "thread local storage" {
const thread1 = try std.Thread.spawn(.{}, testTls, .{});
const thread2 = try std.Thread.spawn(.{}, testTls, .{});
testTls();
thread1.join();
thread2.join();
}
fn testTls() void {
assert(x == 1234);
x += 1;
assert(x == 1235);
}
命令行
$ zig test test_thread_local_variables.zig
1/1 test_thread_local_variables.test.thread local storage...OK
All 1 tests passed.

在单线程构建中,所有线程局部变量均视为普通的命名空间级变量。

线程局部变量不能是 const。

局部变量

局部变量出现在函数、comptime 块及带标签的块内。

局部变量为 const 时,表示初始化后其值不再改变。如果 const 变量的初始值在编译时已知,则变量本身也在 comptime 已知。

局部变量可以使用 comptime 关键字限定。这样其值将在 comptime 已知,且所有读取和写入都发生在程序的语义分析期间,而非运行时。comptime 表达式中声明的所有变量隐式为 comptime 变量。

test_comptime_variables.zig
const std = @import("std");
const expectEqual = std.testing.expectEqual;
test "comptime vars" {
var x: i32 = 1;
comptime var y: i32 = 1;
x += 1;
y += 1;
try expectEqual(2, x);
try expectEqual(2, y);
if (y != 2) {
// 此编译错误永远不会触发,因为 y 是 comptime 变量,
// 因而 `y != 2` 是编译时值,此 if 会静态求值。
@compileError("wrong y value");
}
}
命令行
$ zig test test_comptime_variables.zig
1/1 test_comptime_variables.test.comptime vars...OK
All 1 tests passed.