Hi!
How many times does the import function include the standard library in each translation unit? Is it cached or included in every translation unit, as in C++?
If I worded the question incorrectly, forgive me!
Thanks!
Zig doesn’t use the concept of translation units, so Zig only includes the standard library once in output binaries. So for example, if multiple modules in the same executable @import("std"), they will share the same copy.
1 Like
It’s very good!
1 Like
One nice ‘side effect’ of Zig’s import behavior is that it doesn’t require the @import() to be outside functions, instead you can have decentralized imports right where they are needed, which is sometimes nice for code-generated source files. E.g.:
fn cStrToZig(c_str: [*c]const u8) [:0]const u8 {
return @import("std").mem.span(c_str);
}
2 Likes