From the documentation:
It is also possible to have local variables with static lifetime by using containers inside functions.
const std = @import("std"); const expect = std.testing.expect; test "static local variable" { try expect(foo() == 1235); try expect(foo() == 1236); } fn foo() i32 { const S = struct { var x: i32 = 1234; }; S.x += 1; return S.x; }
Doesn’t this essentially make S
inside foo
a static variable with static memory storage as if it were declared in the file scope, but whose scope is simply limited to foo
?