Importing Files with @import
Bring another source file into scope.
Importing Files with @import is a free Zig Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Bring In Another File
Code grows, so you split it across files. The built-in @import pulls another Zig source file into the current one. 📦
const math = @import("math.zig");It Returns a Value
@import is an expression that gives back a value. You store it in a const, then reach into it with dot access just like a struct.
const math = @import("math.zig");
const r = math.add(2, 3);A File Is a Struct
Every Zig file is secretly a struct. Its top-level declarations become the fields and functions you access after importing it.
Only pub Crosses the Line
A declaration is reachable from another file only if it is marked pub. Without pub, it stays private to its own file.
pub fn add(a: i32, b: i32) i32 {
return a + b;
}Paths Are Relative
A file path inside @import is relative to the importing file. So "util/text.zig" looks in a util folder beside the current file.
const text = @import("util/text.zig");Import the Standard Library
The most common import is std. It is a special module name, not a file path, that gives you the whole standard library.
const std = @import("std");The builtin Module
Another special name is builtin. It exposes compile-time info like the target os and the current optimize mode.
const builtin = @import("builtin");Imports Run at Comptime
@import is resolved entirely at compile time. The path must be a string literal known then, never a value computed at run time.
Cached, Not Duplicated
Import the same file from ten places and Zig loads it once. Every import of one path shares the exact same struct value.
Rename for Clarity
Because the result is just a const, you can rename it freely. Pick a short, clear alias that reads well at every call site.
const fs = @import("std").fs;Reach In Without Storing
You can also chain straight off an import for a one-off use, skipping the intermediate const when you only need one item.
const out = @import("std").io.getStdOut();Quick Check
Recall what makes a top-level item usable from another file.
Recap
You learned that @import loads a file as a struct at compile time, std and builtin are special names, and only pub items cross over. 🎯
Frequently asked questions
Is the “Importing Files with @import” lesson free?
Yes — the full text of “Importing Files with @import” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Importing Files with @import”?
Bring another source file into scope. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Importing Files with @import” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Importing Files with @import
- Structuring a Multi-File Project
- build.zig.zon and the Package Manager
- Adding a Third-Party Module