0Pricing
Zig Academy · درس

استيراد الملفات باستخدام @import

أدخلوا ملف مصدر آخر إلى النطاق

استيراد الملفات باستخدام @import درس مجاني في Zig Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة Zig Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Zig Academy 4 دروس في المجموع.

ماذا ستتعلم في «استيراد الملفات باستخدام @import»؟

أدخلوا ملف مصدر آخر إلى النطاق تتمرن على Zig Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Zig Academy؟

لا تُشترط خبرة سابقة. Zig Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «استيراد الملفات باستخدام @import»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Zig Academy هذا؟

نعم. كل درس في Zig Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. استيراد الملفات باستخدام @import
  2. تنظيم مشروع متعدد الملفات
  3. ‏build.zig.zon ومدير الحزم
  4. إضافة وحدة خارجية
← العودة إلى Zig Academy