နားလည်ထားရမယ့် အချက်
Rust မှာ `let x = 5;` ဖြင့် declare လုပ်ထားတဲ့ variable တိုင်းက default အနေနဲ့ immutable ဖြစ်ပါတယ်—ပြန်ပြီး value ပြောင်းချင်ရင် compiler က error ပြပါတယ်။ ဒါက အခြား language တွေနဲ့ ဆန့်ကျင်ဖက် decision ထင်ရနိုင်ပေမယ့် Rust ရဲ့ safety-first philosophy ကနေ လာတာပါ—variable တစ်ခုက ဘယ်နေရာမှာမှ ပြောင်းလို့မရဘူးဆိုတာ compiler ကသိထားလို့ concurrent code ထဲမှာ ဒီ variable ကို share သုံးရင် data race ဖြစ်နိုင်ချေ ကင်းစင်တယ်ဆိုတာ automatically guarantee ရပါတယ်။ Value ကို ပြောင်းလိုချင်ရင် `let mut x = 5;` ဖြင့် explicit အနေနဲ့ opt-in ဝင်ရပါတယ်—reader တစ်ယောက်က code ကို ဖတ်တဲ့အခါ `mut` keyword တွေ့ရင်ချင်း ဒီ variable ကို ဘယ်နေရာတွင်မှာ ပြောင်းနိုင်တယ်ဆိုတာ instantly သိနိုင်ပါတယ်။ Shadowing ကတော့ `mut` နှင့် လုံးဝမတူပါ—`let x = x + 1;` လို same name ကို `let` ဖြင့် ထပ်ကြေညာခြင်းဖြစ်ပြီး variable အသစ်တစ်ခု ဖန်တီးပြီး name ကို reuse လုပ်တာဖြစ်ပါတယ်—type ကိုတောင် ပြောင်းလို့ရပါတယ် (ဥပမာ string ကနေ integer ဆီ) ဒါပေမယ့် `mut` ကတော့ same variable ရဲ့ value ကိုသာ ပြောင်းနိုင်ပြီး type မပြောင်းနိုင်ပါဘူး။ ဒါကို mental model အနေနဲ့ label maker တစ်ခုနဲ့ တွေးလို့ရပါတယ်—`mut` က box ထဲက content ကို ပြောင်းနိုင်တဲ့ box တစ်ခုဖြစ်ပြီး shadowing ကတော့ label တစ်ခုတည်းကို box အသစ်တစ်ခုပေါ် ထပ်ကပ်လိုက်တာနှင့်တူပါတယ်။
လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်
Lesson content analyzer tool ကို ဆောက်တဲ့အခါ `total_word_count` လို running total ကို accumulate ရမှာဖြစ်လို့ `let mut total_word_count = 0;` ဖြင့် explicitly mutable ဖြစ်အောင် declare ရပါမယ်—file တစ်ခုစီ scan ချင်း value ကို ထပ်ပေါင်းသွားမှာမို့ပါ။ ဒီအတွက် `total_word_count` ကို function တွေဆီ pass လုပ်တဲ့နေရာမှာ ဒီ variable ကို ဘယ်သူတွေ ပြောင်းနိုင်လဲဆိုတာ `mut` keyword ကို scan ကြည့်ရုံနဲ့ ချက်ချင်းသိနိုင်ပါတယ်။ Raw file content ကို string တစ်ခုအနေနဲ့ ဖတ်ပြီးမှ trimmed/lowercased version ကို process လုပ်ချင်ရင်တော့ shadowing ကို သုံးမယ်—`let content = content.trim().to_lowercase();`—variable name တစ်ခုတည်းကို reuse လုပ်ပြီး intermediate step တစ်ခုစီအတွက် name အသစ်တွေ တီထွင်စရာ မလိုတော့ပါ။
အတူတူ စမ်းရေးကြည့်မယ်
fn main() {
let mut total_word_count = 0;
total_word_count += 120;
total_word_count += 85;
println!("Running total: {total_word_count}");
let raw = " Rust Ownership ";
let content = raw.trim().to_lowercase();
println!("Normalized: '{content}'");
}"Running total: 205" နှင့် "Normalized: 'rust ownership'" ကို terminal ထဲ print ထုတ်နိုင်မည်။၅ မိနစ် စမ်းကြည့်
`let mut score = 10;` ကနေစပြီး score ကို 5 ခု ပေါင်းထည့်ပြီး print ထုတ်ပါ—ပြီးရင် `let score = score.to_string();` ဖြင့် shadowing သုံးကာ String type အဖြစ် ပြောင်းပြီး ထပ်ပြသပါ။
သတိလေးတစ်ချက်
Variable ကို ပြန်ပြောင်းလိုချင်ပြီး `mut` ထည့်စရာမေ့ကာ compile error "cannot assign twice to immutable variable" ရရှိခြင်း—Rust default immutability ကို သတိမမူခြင်းကြောင့်ဖြစ်ပါတယ်။
Shadowing ကို `mut` ရဲ့ alternative syntax ချည်းသာလို့ ထင်ပြီး loop ထဲမှာ shadowing ကို repeatedly သုံးခြင်း—shadowing တစ်ခုစီက variable အသစ်တစ်ခု ဖန်တီးနေတာဖြစ်လို့ loop ထဲမှာ accumulate လုပ်ချင်ရင် `mut` ကိုသာ သုံးသင့်ပါတယ်။
The Rust Programming Language — Variables and Mutability — Rust