liveBook · Manning

@import

As explained in Systems Programming with Zig

@import is a Zig builtin function and expression that loads Zig code from a module or file and returns a type containing its declarations.

Zig’s @import loads code from a module or Zig source file. It is a compiler builtin and an expression: it returns a type containing declarations that can be accessed as a namespace. section 2.1 section 2.1.4

For a standard-library import, assign @import("std") to a constant, then use dot syntax to reach declarations such as std.debug.print. The @ prefix marks a compiler-provided builtin. section 2.1

Listing 2.1 A simple program
const std = @import("std"); #1

pub fn main() void { #2
    const x: i32 = 42; #3
    std.debug.print("{}\n", .{x}); #4
}

A small program imports std and uses its debug.print function. section 2.1

A file import can use a relative path, such as @import("fibonacci.zig"). Because the call is an expression, you can select a declaration immediately: @import("fibonacci.zig").fibonacci. The declaration must be marked pub to be accessible from another source file. section 2.1.4

Listing 2.4 main.zig
const std = @import("std");
const fibonacci = @import("fibonacci.zig").fibonacci; #1

pub fn main() void {
    const first = fibonacci(1);
    const second = fibonacci(2);
    const third = fibonacci(3);
    const fourth = fibonacci(4);
    std.debug.print("{} {} {} {}\n", .{ first, second, third, fourth });
}

Import a function directly from a neighboring Zig file and call it from main. section 2.1.4

Named modules can also be connected in a build script. For example, test_mod.addImport("vm", vm_mod) makes @import("vm") available to files in the test module. Within build scripts, @import can also access code from dependencies’ build scripts; translated C code can be registered as a module and imported by name. section 6.3.1 section 7.2.1

Listing 6.2 A more modular build script
const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{}); #1
    const optimize = b.standardOptimizeOption(.{}); #1

    const vm_mod = b.createModule(.{ #2
        .root_source_file = b.path("src/vm.zig"),
        .target = target,
        .optimize = optimize,
    });

    const test_mod = b.createModule(.{
        .root_source_file = b.path("src/tests.zig"),
        .target = target,
        .optimize = optimize,
    });
    test_mod.addImport("vm", vm_mod); #3
}

The build script creates VM and test modules, then connects them under the name vm. section 6.3.1

How Systems Programming with Zig covers it

How deep it goes
Explains imports as expressions returning namespace types, including relative paths and pub visibility.
Examples
Uses standard-library printing, a Fibonacci function imported from a file, and a VM module connected to tests.
What it assumes
Assumes familiarity with imperative program structure while introducing Zig builtins, modules, and namespaces.
What you can do after
Apply it via Zig’s compiler to access declarations from files and named modules.
How it connects
Connects modules and namespaces with public declarations, the build system, and C translation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.