0Pricing
Learn Rust Coding · درس

تعمّق في match

الأنماط والحراس

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

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

The match Expression

match compares a value against a series of patterns and runs the arm of the first match. It is an expression, so it returns a value.

fn main() {
    let n = 3;
    let label = match n {
        1 => "one",
        2 => "two",
        3 => "three",
        _ => "many",
    };
    println!("{label}");
}

Exhaustiveness

A match must cover every possible value. The wildcard _ catches anything not matched by earlier arms, ensuring exhaustiveness.

fn main() {
    let x = 42;
    match x {
        0 => println!("zero"),
        _ => println!("nonzero"),
    }
}

Matching Multiple Values

Use | to match several patterns in one arm.

fn main() {
    let c = 'e';
    match c {
        'a' | 'e' | 'i' | 'o' | 'u' => println!("vowel"),
        _ => println!("consonant"),
    }
}

Range Patterns

Match a range of values with ..= (inclusive). Great for grouping numbers or characters.

fn main() {
    let score = 85;
    let grade = match score {
        90..=100 => 'A',
        80..=89 => 'B',
        70..=79 => 'C',
        _ => 'F',
    };
    println!("Grade: {grade}");
}

Match Guards

A guard is an extra if condition on an arm. The arm matches only when the pattern fits and the guard is true.

fn main() {
    let pair = (2, -2);
    match pair {
        (x, y) if x + y == 0 => println!("sum is zero"),
        (x, _) if x % 2 == 0 => println!("first is even"),
        _ => println!("no rule"),
    }
}

Binding in Patterns

Patterns can bind matched parts to variables for use in the arm body.

fn main() {
    let msg = Some(7);
    match msg {
        Some(n) => println!("got {n}"),
        None => println!("nothing"),
    }
}

Matching Enums

match shines with enums, especially when variants carry data.

enum Shape {
    Circle(f64),
    Rectangle(f64, f64),
}

fn area(s: Shape) -> f64 {
    match s {
        Shape::Circle(r) => 3.14159 * r * r,
        Shape::Rectangle(w, h) => w * h,
    }
}

fn main() {
    println!("{}", area(Shape::Rectangle(3.0, 4.0)));
}

Ignoring with _ and ..

Use _ to ignore a single value and .. to ignore the rest of a tuple or struct.

fn main() {
    let triple = (1, 2, 3);
    match triple {
        (first, ..) => println!("first is {first}"),
    }
}

Returning From match

Because match is an expression, you can assign its result or return it directly. Every arm must yield the same type.

fn describe(n: i32) -> &'static str {
    match n.cmp(&0) {
        std::cmp::Ordering::Less => "negative",
        std::cmp::Ordering::Equal => "zero",
        std::cmp::Ordering::Greater => "positive",
    }
}

fn main() {
    println!("{}", describe(-5));
}

Arm Order Matters

Arms are checked top to bottom; the first matching one wins. Put specific patterns before general ones and the wildcard last.

Binding Ranges with @

Combine a range with the @ operator to test a value and keep its exact contents in the same arm.

fn main() {
    let n = 42;
    match n {
        small @ 0..=9 => println!("single digit: {small}"),
        big @ 10..=99 => println!("two digits: {big}"),
        _ => println!("large"),
    }
}

Quick Check

What is the purpose of a match guard like (x, y) if x > y?

Recap

You went deep on match:

  • It is an exhaustive expression returning a value
  • | matches multiple patterns; ..= matches ranges
  • Guards add extra if conditions
  • Patterns bind data; _ and .. ignore parts
  • Arm order matters — first match wins

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

هل درس «تعمّق في match» مجاني؟

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

ماذا ستتعلم في «تعمّق في match»؟

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

هل أحتاج إلى خبرة سابقة لأبدأ Learn Rust Coding؟

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

كم من الوقت يستغرق درس «تعمّق في match»؟

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

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

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

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

  1. تعمّق في match
  2. if let وwhile let
  3. الربط باستخدام @
  4. التفكيك البنيوي
← العودة إلى Learn Rust Coding