Thuta Learning
ရှာဖွေရန်
Rust
IntermediateProgrammingbeginner

Enums, `match`, နှင့် `Option<T>`

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Enums, `match`, နှင့် `Option<T>` concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • နမူနာ Rust code ကို ကိုယ်တိုင် run ပြီး output စစ်နိုင်ရန်
  • Tutorial Platform project နှင့် production scenario တွင် မှန်ကန်စွာအသုံးချနိုင်ရန်

နားလည်ထားရမယ့် အချက်

Enum ဆိုတာ value တစ်ခုက possible variant အများအပြားထဲက တစ်ခုသာ ဖြစ်နိုင်ကြောင်း ဖော်ပြတဲ့ type ဖြစ်ပြီး Rust ရဲ့ enum က C-style enum (integer constant များ) ထက်ပိုပြီး powerful ပါတယ်—variant တစ်ခုစီက data ကိုယ်ပိုင် ပါဆောင်နိုင်ပါတယ် (`enum Difficulty { Basic, Intermediate, Advanced(String) }` လို)။ `match` expression ကတော့ enum ကို handle လုပ်ဖို့ primary tool ဖြစ်ပြီး Rust compiler က exhaustiveness check လုပ်ပေးပါတယ်—variant တစ်ခုကို handle လုပ်ဖို့ မေ့ခဲ့ရင် compile error တက်လာမှာဖြစ်လို့ switch statement မှာ case တစ်ခု fall-through ဖြစ်သွားတတ်တဲ့ bug ကို compile time မှာပင် ကာကွယ်ပေးပါတယ်။ Rust မှာ null pointer concept လုံးဝမရှိပါဘူး—အခြား language တွေမှာ "billion-dollar mistake" လို့ ခေါ်လေ့ရှိတဲ့ NullPointerException ကို ရှောင်ဖို့ Rust က `Option<T>` enum (`Some(T)` သို့ `None`) ကို standard library ထဲ ပေးထားပါတယ်—value ရှိချင်ရှိမယ်, မရှိချင် မရှိမယ်ဆိုတဲ့ possibility ကို type system ထဲမှာ explicit ဖော်ပြထားလို့ compiler က `Option<T>` ကို unwrap မလုပ်ဘဲ underlying value ကို တိုက်ရိုက်သုံးခွင့် လုံးဝမပေးပါဘူး—developer က `None` case ကို handle လုပ်ဖို့ compile time မှာ တွန်းအားပေးခံရပါတယ်။ ဒါကို postal mailbox တစ်ခုနှင့် တွေးလို့ရပါတယ်—mail ရောက်ချင်ရောက်မယ် (Some), မရောက်ချင် ဗလာနေမယ် (None)—mailbox ကို ဖွင့်ကြည့်ဖို့ ကိုယ့်ဘာသာ decide ရပါတယ်၊ mail ရှိမယ်လို့ blindly assume ပြီး လက်ကို ထည့်လိုက်ရင် (null pointer dereference လို) ပြဿနာဖြစ်နိုင်ပါတယ်—Rust ရဲ့ `Option<T>` က ဒီ "ဖွင့်ကြည့်ခြင်း" step ကို compile time မှာ mandatory ဖြစ်စေပါတယ်။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Content analyzer ရဲ့ `Difficulty` ကို `enum Difficulty { Basic, Intermediate, Advanced }` အဖြစ် define ပြီး Lesson 9 ရဲ့ `String` field အစား ဒီ enum ကို သုံးမယ်—typo ("Bassic" လို misspelled string) ဖြစ်နိုင်ခြေကို type level မှာ လုံးဝ ဖယ်ရှားပေးမှာပါ။ Lesson file တစ်ခုကနေ tag ကို parse ထုတ်ချင်ရင် tag မတွေ့ရင်ဖြစ်နိုင်လို့ `fn find_tag(text: &str) -> Option<&str>` return type သုံးမယ်—caller ဘက်က `match find_tag(text) { Some(tag) => ..., None => ... }` ဖြင့် handle မဖြစ်မနေ လုပ်ရပါမယ်—tag ရှိတယ်လို့ assume ပြီး unwrap ချည်း သုံးရင် None case မှာ panic ဖြစ်နိုင်ပါတယ်။ Difficulty enum ကို `match` ဖြင့် display string ထုတ်ချင်ရင် variant အားလုံး handle ဖြစ်မှသာ compile ဖြစ်မှာဖြစ်လို့ variant အသစ်တစ်ခု (`Expert`) ထပ်ထည့်ရင် ဒီ `match` ကို update လုပ်ဖို့ compiler ကိုယ်တိုင် သတိပေးပါလိမ့်မယ်။

အတူတူ စမ်းရေးကြည့်မယ်

rust
enum Difficulty {
    Basic,
    Intermediate,
    Advanced,
}

fn label(d: &Difficulty) -> &str {
    match d {
        Difficulty::Basic => "basic",
        Difficulty::Intermediate => "intermediate",
        Difficulty::Advanced => "advanced",
    }
}

fn find_tag(text: &str) -> Option<&str> {
    text.split_whitespace().find(|w| w.starts_with('#'))
}

fn main() {
    println!("{}", label(&Difficulty::Intermediate));
    match find_tag("Rust ownership #systems") {
        Some(tag) => println!("tag found: {tag}"),
        None => println!("no tag found"),
    }
}
You should see
"intermediate" နှင့် "tag found: #systems" ကို print ထုတ်နိုင်မည်။

၅ မိနစ် စမ်းကြည့်

`enum ContentKind { Text, Code(String), Diagram }` ဖန်တီးပါ—`Code` variant ကို language name ပါဆောင်စေပါ—`match` expression ဖြင့် variant တစ်ခုစီအတွက် description string ထုတ်ပေးမည့် function တစ်ခု ရေးပါ။

သတိလေးတစ်ချက်

`Option<T>` ကို `.unwrap()` ဖြင့် None case ကို handle မလုပ်ဘဲ blindly unwrap ခြင်း—value ရှိမယ်လို့ 100% သေချာမှသာ unwrap သင့်ပြီး မဟုတ်ရင် runtime panic ဖြစ်နိုင်ပါတယ်။

`match` ထဲမှာ enum variant အားလုံးကို handle မလုပ်ဘဲ `_` wildcard ကို default အနေနဲ့ အလွယ်တကူထည့်ခြင်း—variant အသစ်ထည့်လာချိန်မှာ compiler ရဲ့ exhaustiveness warning ကို ဆုံးရှုံးသွားပြီး logic error ကို compile time မှာ catch မရနိုင်တော့ပါ။

The Rust Programming Language — Defining an EnumRust

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • `Option<T>` ကို `.unwrap()` ဖြင့် None case ကို handle မလုပ်ဘဲ blindly unwrap ခြင်း—value ရှိမယ်လို့ 100% သေချာမှသာ unwrap သင့်ပြီး မဟုတ်ရင် runtime panic ဖြစ်နိုင်ပါတယ်။
  • `match` ထဲမှာ enum variant အားလုံးကို handle မလုပ်ဘဲ `_` wildcard ကို default အနေနဲ့ အလွယ်တကူထည့်ခြင်း—variant အသစ်ထည့်လာချိန်မှာ compiler ရဲ့ exhaustiveness warning ကို ဆုံးရှုံးသွားပြီး logic error ကို compile time မှာ catch မရနိုင်တော့ပါ။
  • နမူနာ code ကို production system ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test environment တွင် အရင်အတည်ပြုပါ။

လေ့ကျင့်ခန်း

`enum ContentKind { Text, Code(String), Diagram }` ဖန်တီးပါ—`Code` variant ကို language name ပါဆောင်စေပါ—`match` expression ဖြင့် variant တစ်ခုစီအတွက် description string ထုတ်ပေးမည့် function တစ်ခု ရေးပါ။

You'll know it worked when: "intermediate" နှင့် "tag found: #systems" ကို print ထုတ်နိုင်မည်။

Enums, `match`, နှင့် `Option<T>` | Thuta Learning