0Pricing
Zig Academy · レッスン

@importでファイルをインポートする

別のソースファイルをスコープに取り込みます。

「@importでファイルをインポートする」はCoddyKit上の無料Zig Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはZig Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Zig Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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. 🎯

よくある質問

「@importでファイルをインポートする」レッスンは無料ですか?

はい。「@importでファイルをインポートする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Zig Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Zig Academyコースには全4レッスンが含まれています。

「@importでファイルをインポートする」で何を学びますか?

別のソースファイルをスコープに取り込みます。 ブラウザで直接実行するハンズオンコードでZig Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Zig Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのZig Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「@importでファイルをインポートする」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このZig Academyレッスンでコードを書いて実行できますか?

はい。すべてのZig Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. @importでファイルをインポートする
  2. 複数ファイルプロジェクトの構成
  3. build.zig.zonとパッケージマネージャー
  4. サードパーティモジュールの追加
← Zig Academyに戻る