usingnamespace

usingnamespace is a declaration that mixes all the public declarations of the operand, which must be a struct, union, enum, or opaque, into the namespace:

usingnamespace.zig

  1. test "using std namespace" {
  2. const S = struct {
  3. usingnamespace @import("std");
  4. };
  5. try S.testing.expect(true);
  6. }

Shell

  1. $ zig test usingnamespace.zig
  2. 1/1 test "using std namespace"... OK
  3. All 1 tests passed.

usingnamespace has an important use case when organizing the public API of a file or package. For example, one might have c.zig with all of the C imports:

c.zig

  1. pub usingnamespace @cImport({
  2. @cInclude("epoxy/gl.h");
  3. @cInclude("GLFW/glfw3.h");
  4. @cDefine("STBI_ONLY_PNG", "");
  5. @cDefine("STBI_NO_STDIO", "");
  6. @cInclude("stb_image.h");
  7. });

The above example demonstrates using pub to qualify the usingnamespace additionally makes the imported declarations pub. This can be used to forward declarations, giving precise control over what declarations a given file exposes.