0Pricing
Zig Academy · درس

إلغاء التغليف بأمان باستخدام if وorelse

تعاملوا مع null دون مفاجآت

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

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

You Must Unwrap First

An optional is not the value inside it. Before you can use the contents you have to unwrap it and deal with the null case.

Capture with if

An if on an optional can bind the inner value with a pipe capture. The body runs only when the optional is not null.

if (maybe) |value| {
    use(value);
}

Handle the null Branch

Add an else to react when the optional is null. Now both outcomes are covered with no chance of a missing case.

if (maybe) |v| {
    use(v);
} else {
    handleEmpty();
}

The Capture Is Unwrapped

Inside the if body the captured name is the plain inner type, not an optional. The question mark is already gone for you.

if (age) |a| {
    // a is i32, not ?i32
}

orelse Supplies a Default

The orelse operator unwraps an optional, but if it is null it gives back the value on its right instead.

const a = age orelse 0;

orelse Is an Expression

Because orelse returns a value, you can use it inline anywhere, like passing a defaulted number straight into a function call.

print(level orelse 1);

orelse Can Run Code

The right side of orelse may be a block. Use it to compute a fallback or even return early from the current function.

const v = lookup() orelse return;

Force Unwrap with .?

Writing .? asserts the optional is not null and hands back the value. If it is null in a safe build, your program panics.

const a = age.?;

Use .? Only When Certain

Reach for .? only when null is truly impossible. Otherwise prefer if or orelse so an empty value never crashes you.

Choose by Intent

Use if to branch on presence, orelse for a quick default, and .? only when you can prove the value is there.

Safety Is the Default

Each tool forces you to acknowledge null. That is why Zig optionals rarely turn into the surprise crashes seen in other languages.

Quick Check

You want a default of 0 when an optional integer is null. Which fits best?

Recap

You can unwrap optionals with if captures, supply defaults via orelse, or assert with .?. Each one makes you face null on purpose. ✅

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

هل درس «إلغاء التغليف بأمان باستخدام if وorelse» مجاني؟

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

ماذا ستتعلم في «إلغاء التغليف بأمان باستخدام if وorelse»؟

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

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

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

كم من الوقت يستغرق درس «إلغاء التغليف بأمان باستخدام if وorelse»؟

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

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

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

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

  1. التصريح عن الأنواع الاختيارية
  2. إلغاء التغليف بأمان باستخدام if وorelse
  3. المؤشرات الاختيارية و?*T
  4. ‏null مقابل undefined
← العودة إلى Zig Academy