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

Data Types — Scalar နှင့် Compound

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

  • Data Types — Scalar နှင့် Compound concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • နမူနာ Rust code ကို ကိုယ်တိုင် run ပြီး output စစ်နိုင်ရန်
  • Tutorial Platform project နှင့် production scenario တွင် မှန်ကန်စွာအသုံးချနိုင်ရန်

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

Rust ဟာ statically typed language ဖြစ်ပြီး variable တိုင်းရဲ့ type ကို compile time မှာ သိထားရပါတယ်—compiler က context ကနေ type ကို infer လုပ်ပေးနိုင်ပေမယ့် (`let x = 5;` ကို `i32` လို့ default သတ်မှတ်သလို) ambiguous ဖြစ်တဲ့ context တွေမှာတော့ annotation (`let x: u32 = 5;`) ရေးပေးရပါတယ်။ Scalar type လေးမျိုးရှိပါတယ်—integer (`i8` ကနေ `i128` အထိ signed, `u8` ကနေ `u128` အထိ unsigned, size အလိုက် bit width တိကျစွာသတ်မှတ်ထား), floating-point (`f32`, `f64`), boolean (`bool`), character (`char`, Unicode scalar value တစ်ခုကို ကိုယ်စားပြု 4 bytes)။ Compound type နှစ်မျိုးက tuple (fixed-length, type အမျိုးမျိုးရောနိုင်သော group—`(i32, f64, char)`) နှင့် array (fixed-length, same-type elements—`[i32; 5]`) ဖြစ်ပါတယ်။ ဒီ type တွေအားလုံးရဲ့ တူညီတဲ့ characteristic တစ်ခုက compile time မှာ size ကို အတိအကျ သိထားနိုင်တာဖြစ်လို့ default အားဖြင့် stack ပေါ်မှာ direct သိမ်းနိုင်ပြီး—heap allocation, pointer indirection မလိုအပ်ဘဲ access မြန်ပါတယ်။ ဒါက dynamically-sized data (`String`, `Vec<T>`) ကို heap ပေါ်မှာ သိမ်းရတာနှင့် ဆန့်ကျင်ဖက်ဖြစ်ပြီး—Lesson 8 မှာ `String` ကို လေ့လာတဲ့အခါ ဒီ contrast ကို ပြန်တွေ့ရမှာဖြစ်ပါတယ်။

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

Content analyzer tool ထဲမှာ file တစ်ခုစီရဲ့ statistics ကို `(String, u32, f64)` tuple အနေနဲ့ ပြန်ပေးမယ်—file name, word count, average word length ကို group တစ်ခုတည်းအနေနဲ့ ဆောင်သွားနိုင်ပါတယ်။ Difficulty level တစ်ခုစီအတွက် lesson count ကို track ချင်ရင် `[u32; 3]` array (Basic, Intermediate, Advanced index) သုံးနိုင်ပြီး size ကို compile time မှာသိထားလို့ bound-check ကို run-time efficient ဖြစ်စေပါတယ်။ Word count field တွေအားလုံးကို `u32` (unsigned, negative မလိုအပ်) ဖြင့် declare ထားမယ်—`i32` သုံးရင် negative count ဆိုတာ conceptually မဖြစ်နိုင်တာကို type level မှာ ကာကွယ်ပေးမှာ မဟုတ်တော့ပါ။

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

rust
fn main() {
    let stats: (String, u32, f64) = (String::from("ownership.md"), 842, 5.3);
    let (name, word_count, avg_len) = stats;
    println!("{name}: {word_count} words, avg length {avg_len}");

    let lessons_per_level: [u32; 3] = [7, 7, 6];
    println!("Basic lessons: {}", lessons_per_level[0]);
}
You should see
"ownership.md: 842 words, avg length 5.3" နှင့် "Basic lessons: 7" ကို print ထုတ်နိုင်မည်။

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

`(String, u32)` tuple တစ်ခုနှင့် `[u32; 5]` array တစ်ခု ဖန်တီးပါ—tuple ထဲက value နှစ်ခုကို destructure လုပ်ပြီး array ရဲ့ element သုံးခုကို index ဖြင့် print ထုတ်ပါ။

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

Integer type default (`i32`) ကို negative value မဖြစ်နိုင်တဲ့ count/length field များအတွက်ပါ blanket သုံးခြင်း—`u32` သုံးရင် compile time မှာပင် logical constraint ကို enforce လို့ရပေမယ့် အလေ့ကျင့်ဖြင့် `i32` ချည်း သုံးမိတတ်ပါတယ်။

Array index ကို compile time size ထက် ကျော်ပြီး access ကြိုးစားခြင်း (ဥပမာ `[u32; 3]` ရဲ့ index 3)—Rust က ဒါကို runtime panic ဖြင့် catch ပေးပေမယ့် logic error ကို compile time မှာ catch မရနိုင်တာကို သတိထားရပါမယ်။

The Rust Programming Language — Data TypesRust

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

  • Integer type default (`i32`) ကို negative value မဖြစ်နိုင်တဲ့ count/length field များအတွက်ပါ blanket သုံးခြင်း—`u32` သုံးရင် compile time မှာပင် logical constraint ကို enforce လို့ရပေမယ့် အလေ့ကျင့်ဖြင့် `i32` ချည်း သုံးမိတတ်ပါတယ်။
  • Array index ကို compile time size ထက် ကျော်ပြီး access ကြိုးစားခြင်း (ဥပမာ `[u32; 3]` ရဲ့ index 3)—Rust က ဒါကို runtime panic ဖြင့် catch ပေးပေမယ့် logic error ကို compile time မှာ catch မရနိုင်တာကို သတိထားရပါမယ်။
  • နမူနာ code ကို production system ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test environment တွင် အရင်အတည်ပြုပါ။

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

`(String, u32)` tuple တစ်ခုနှင့် `[u32; 5]` array တစ်ခု ဖန်တီးပါ—tuple ထဲက value နှစ်ခုကို destructure လုပ်ပြီး array ရဲ့ element သုံးခုကို index ဖြင့် print ထုတ်ပါ။

You'll know it worked when: "ownership.md: 842 words, avg length 5.3" နှင့် "Basic lessons: 7" ကို print ထုတ်နိုင်မည်။

Data Types — Scalar နှင့် Compound | Thuta Learning