0Pricing
Zig Academy · درس

استخدام const للقيم التي لا تتغير أبداً

ارتباطات غير قابلة للتغيير افتراضياً

استخدام const للقيم التي لا تتغير أبداً درس مجاني في Zig Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Zig Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Zig Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Bindings Start Here

In Zig, you give a value a name with a binding. The first choice you make is whether that name is allowed to change later.

Meet const

The keyword const creates a name whose value can never be reassigned. It is the safe, default way to name things in Zig.

const pi = 3.14159;

Immutable by Choice

A const binding is immutable: once it points at a value, that link is locked. This makes your code easier to read and reason about.

Reassigning Is an Error

Try to give a const a new value and the compiler stops you. Zig reports a clear error instead of silently letting the bug through.

const x = 5;
x = 6; // error: cannot assign to constant

Initialize at Declaration

You set a const's value right where you declare it. There is no empty const waiting to be filled in later on.

const greeting = "Hello, Zig";

const for Configuration

Fixed numbers like a buffer size or a max count are perfect for const. They name a value clearly and guarantee it stays put.

const max_users = 100;

Prefer const First

Good Zig style reaches for const first and only loosens up when a value truly must change. Less mutation means fewer surprises.

Type Is Still Inferred

You usually do not write a type after const. Zig infers it from the value, so const count = 0 just works as an integer.

const count = 0;

const at the Top Level

Constants declared outside any function become file-wide globals. They are evaluated once and shared across your whole program.

const version = "1.0.0";

Importing Returns a const

You will often see const std = @import. The result of an import is a value, and binding it with const keeps that module fixed.

const std = @import("std");

Self-Documenting Code

Seeing const tells any reader the value will not move. That single keyword acts as a small, trustworthy promise about your intent.

Quick Check

You declare const limit = 10. What happens if you later write limit = 20?

Recap

You learned that const names a value that never changes. Initialize it once, let Zig infer the type, and reach for it as your default. 🔒

الأسئلة الشائعة

هل درس «استخدام const للقيم التي لا تتغير أبداً» مجاني؟

نعم — نص درس «استخدام const للقيم التي لا تتغير أبداً» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Zig Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Zig Academy 4 دروس في المجموع.

ماذا ستتعلم في «استخدام const للقيم التي لا تتغير أبداً»؟

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

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

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

كم من الوقت يستغرق درس «استخدام const للقيم التي لا تتغير أبداً»؟

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

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

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

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

  1. استخدام const للقيم التي لا تتغير أبداً
  2. استخدام var للحالة القابلة للتغيير
  3. استنتاج النوع مقابل الأنواع الصريحة
  4. ‏undefined والذاكرة غير المهيأة
← العودة إلى Zig Academy