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

Functions နှင့် Control Flow as Expressions

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

  • Functions နှင့် Control Flow as Expressions concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • နမူနာ Rust code ကို ကိုယ်တိုင် run ပြီး output စစ်နိုင်ရန်
  • Tutorial Platform project နှင့် production scenario တွင် မှန်ကန်စွာအသုံးချနိုင်ရန်

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

Rust မှာ function တစ်ခုရဲ့ body ဟာ statement (effect ရှိပေမယ့် value မပြန်) များနှင့် expression (value ပြန်) များ ရောနေတဲ့ block ဖြစ်ပါတယ်—block တစ်ခုရဲ့ နောက်ဆုံး line က semicolon မပါဘဲ expression တစ်ခု ဆိုရင် ဒီ expression ရဲ့ value ကိုပဲ block တစ်ခုလုံးရဲ့ return value အဖြစ် implicit အနေနဲ့ ပြန်ပေးပါတယ်—`return` keyword ကို explicit ရေးစရာ မလိုအပ်ဘဲ။ ဒါက C/Java လို language တွေနှင့် သိသိသာသာ ကွာခြားချက်တစ်ခုဖြစ်ပြီး Rust ရဲ့ "most things are expressions" philosophy ရဲ့ ရလဒ်တစ်ခုပါ—`if` ကိုတောင် statement မဟုတ်ဘဲ expression အဖြစ် သုံးလို့ရပါတယ် (`let x = if condition { 5 } else { 6 };`)—branch နှစ်ခုစလုံးက type တူညီရမယ်ဆိုတဲ့ constraint ရှိပါတယ်။ Loop သုံးမျိုးရှိပါတယ်—`loop` (explicit `break` ခံရမှသာ ရပ်တဲ့ infinite loop, `break value;` ဖြင့် value ကို loop ရဲ့ result အဖြစ် ပြန်ပေးလို့ရ), `while` (condition true ဖြစ်နေသရွေ့ run), `for` (iterator တစ်ခုကို traverse, index-based error—off-by-one—ကို ရှောင်ရှားပေးပါတယ်)။ Function signature ထဲက parameter type/return type ကို explicit ရေးရမယ်ဆိုတာကလည်း documentation တစ်ခုအနေနဲ့ ကိုယ်တိုင် အလုပ်လုပ်ပြီး caller ဘက်က ဘာ type expect လုပ်ရမလဲ compiler error အဖြစ်ချက်ချင်း သိနိုင်ပါတယ်။

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

Content analyzer ရဲ့ `count_words` function ကို `fn count_words(text: &str) -> u32 { text.split_whitespace().count() as u32 }` လို ရေးမယ်—curly brace အတွင်းက expression ဟာ semicolon မပါလို့ implicit return value ဖြစ်ပါတယ်။ Difficulty level ကို tag string ကနေ classify ချင်ရင် `if`/`else if`/`else` chain ကို expression အနေနဲ့ သုံးမယ်—`let level = if word_count < 500 { "basic" } else if word_count < 1500 { "intermediate" } else { "advanced" };`—branch တိုင်း `&str` type တူညီအောင် ဆက်ရေးရပါတယ်။ Lesson file directory ထဲက file တွေအားလုံးကို `for entry in files { ... }` ဖြင့် iterate လုပ်ပြီး word count ကို accumulate မယ်—index management manual လုပ်စရာ မလိုတော့ပါ။

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

rust
fn count_words(text: &str) -> u32 {
    text.split_whitespace().count() as u32
}

fn classify(word_count: u32) -> &'static str {
    if word_count < 500 {
        "basic"
    } else if word_count < 1500 {
        "intermediate"
    } else {
        "advanced"
    }
}

fn main() {
    let count = count_words("Rust ownership prevents data races");
    println!("{count} words -> {}", classify(count));

    let mut n = 0;
    let result = loop {
        n += 1;
        if n == 5 { break n * 10; }
    };
    println!("loop result: {result}");
}
You should see
"5 words -> basic" နှင့် "loop result: 50" ကို print ထုတ်နိုင်မည်။

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

`fn classify_length(n: u32) -> &'static str` function ရေးပါ—`n` < 3 ဆိုရင် "short", < 8 ဆိုရင် "medium", မဟုတ်ရင် "long" ပြန်ပေးရမယ်—`if` expression တစ်ခုတည်းသာသုံးပြီး `return` keyword လုံးဝမသုံးပါ။

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

Function ရဲ့ နောက်ဆုံး expression ကို semicolon ထပ်ထည့်ပြီး implicit return value ဖြစ်စေမယ့်အစား `()` (unit type) ပြန်စေခြင်း—return type ကွာနေတယ်ဆိုတဲ့ compile error နှင့် ရင်ဆိုင်ရနိုင်ပါတယ်။

`for` loop ထဲက collection ကို index-based (`for i in 0..vec.len() { vec[i] }`) style ဖြင့်သာ ရေးနေခြင်း—iterator-based (`for item in &vec`) ကို မသုံးဘဲ off-by-one bug ဖြစ်နိုင်ခြေနှင့် code ကို ပိုရှုပ်သွားစေနိုင်ပါတယ်။

The Rust Programming Language — Control FlowRust

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

  • Function ရဲ့ နောက်ဆုံး expression ကို semicolon ထပ်ထည့်ပြီး implicit return value ဖြစ်စေမယ့်အစား `()` (unit type) ပြန်စေခြင်း—return type ကွာနေတယ်ဆိုတဲ့ compile error နှင့် ရင်ဆိုင်ရနိုင်ပါတယ်။
  • `for` loop ထဲက collection ကို index-based (`for i in 0..vec.len() { vec[i] }`) style ဖြင့်သာ ရေးနေခြင်း—iterator-based (`for item in &vec`) ကို မသုံးဘဲ off-by-one bug ဖြစ်နိုင်ခြေနှင့် code ကို ပိုရှုပ်သွားစေနိုင်ပါတယ်။
  • နမူနာ code ကို production system ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test environment တွင် အရင်အတည်ပြုပါ။

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

`fn classify_length(n: u32) -> &'static str` function ရေးပါ—`n` < 3 ဆိုရင် "short", < 8 ဆိုရင် "medium", မဟုတ်ရင် "long" ပြန်ပေးရမယ်—`if` expression တစ်ခုတည်းသာသုံးပြီး `return` keyword လုံးဝမသုံးပါ။

You'll know it worked when: "5 words -> basic" နှင့် "loop result: 50" ကို print ထုတ်နိုင်မည်။

Functions နှင့် Control Flow as Expressions | Thuta Learning