Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ชนิดข้อมูล (Data Types)

Rust เป็นภาษา statically typed หมายความว่าต้องรู้ชนิดของตัวแปรทั้งหมดตอน compile

ประเภทของข้อมูล

Rust แบ่งชนิดข้อมูลเป็น 2 กลุ่มหลัก:

  1. Scalar Types - ค่าเดี่ยว
  2. Compound Types - ค่าหลายค่ารวมกัน

Scalar Types

1. Integer (จำนวนเต็ม)

ขนาดSigned (มีลบ)Unsigned (บวกเท่านั้น)
8-biti8u8
16-biti16u16
32-biti32u32
64-biti64u64
128-biti128u128
archisizeusize
fn main() {
    let a: i32 = 42;        // จำนวนเต็ม 32-bit
    let b: u8 = 255;        // จำนวนเต็มบวก 8-bit (0-255)
    let c = 1000;           // i32 (default)
    let d: isize = -500;    // ขึ้นกับ CPU (32 หรือ 64 bit)

    println!("a={}, b={}, c={}, d={}", a, b, c, d);
}

รูปแบบการเขียนตัวเลข

fn main() {
    let decimal = 98_222;      // Decimal (ใส่ _ ให้อ่านง่าย)
    let hex = 0xff;            // Hexadecimal
    let octal = 0o77;          // Octal
    let binary = 0b1111_0000;  // Binary
    let byte = b'A';           // Byte (u8 only)

    println!("{}, {}, {}, {}, {}", decimal, hex, octal, binary, byte);
    // 98222, 255, 63, 240, 65
}

2. Floating-Point (ทศนิยม)

fn main() {
    let x = 2.0;      // f64 (default, แม่นยำกว่า)
    let y: f32 = 3.0; // f32 (ใช้หน่วยความจำน้อยกว่า)

    println!("x={}, y={}", x, y);
}

การคำนวณ

fn main() {
    let sum = 5 + 10;           // บวก
    let difference = 95.5 - 4.3; // ลบ
    let product = 4 * 30;        // คูณ
    let quotient = 56.7 / 32.2;  // หาร
    let remainder = 43 % 5;      // เศษ (modulo)

    println!("sum={}", sum);               // 15
    println!("difference={}", difference); // 91.2
    println!("product={}", product);       // 120
    println!("quotient={}", quotient);     // 1.76...
    println!("remainder={}", remainder);   // 3
}

3. Boolean (ค่าความจริง)

fn main() {
    let t = true;
    let f: bool = false;

    println!("t={}, f={}", t, f);

    // ใช้ในเงื่อนไข
    if t {
        println!("This is true!");
    }
}

4. Character (ตัวอักษร)

fn main() {
    let c = 'z';
    let z: char = 'ℤ';
    let heart_eyed_cat = '😻';
    let thai = 'ก';

    println!("{}, {}, {}, {}", c, z, heart_eyed_cat, thai);
}

หมายเหตุ: char ใน Rust ใช้ 4 bytes และรองรับ Unicode ทั้งหมด ใช้ single quote 'a' (ไม่ใช่ double quote "a")


Compound Types

1. Tuple (ทูเพิล)

Tuple รวมค่าหลายชนิดไว้ด้วยกัน มีขนาดคงที่

fn main() {
    let tup: (i32, f64, u8) = (500, 6.4, 1);

    // Destructuring - แยกค่าออกมา
    let (x, y, z) = tup;
    println!("x={}, y={}, z={}", x, y, z);

    // เข้าถึงด้วย index
    let five_hundred = tup.0;
    let six_point_four = tup.1;
    let one = tup.2;

    println!("{}, {}, {}", five_hundred, six_point_four, one);
}

Unit Type

Tuple ว่าง () เรียกว่า unit ใช้แทน “ไม่มีค่า”

fn main() {
    let unit: () = ();
    println!("unit = {:?}", unit); // ()
}

2. Array (อาร์เรย์)

Array รวมค่าชนิดเดียวกันไว้ด้วยกัน มีขนาดคงที่

fn main() {
    // ประกาศ array
    let a = [1, 2, 3, 4, 5];

    // ระบุชนิดและขนาด
    let b: [i32; 5] = [1, 2, 3, 4, 5];

    // สร้าง array ที่มีค่าเหมือนกันทั้งหมด
    let c = [3; 5]; // [3, 3, 3, 3, 3]

    println!("a = {:?}", a);
    println!("b = {:?}", b);
    println!("c = {:?}", c);
}

การเข้าถึง Elements

fn main() {
    let a = [1, 2, 3, 4, 5];

    let first = a[0];  // 1
    let second = a[1]; // 2

    println!("first={}, second={}", first, second);
}

คำเตือน: ถ้าเข้าถึง index ที่ไม่มีอยู่ โปรแกรมจะ panic!

let a = [1, 2, 3];
let x = a[10]; // ❌ panic!

Type Annotation vs Type Inference

Rust สามารถเดาชนิดข้อมูลได้:

fn main() {
    // Type Inference - Rust เดาให้
    let x = 5;       // i32
    let y = 2.0;     // f64
    let z = true;    // bool

    // Type Annotation - ระบุเอง
    let a: i64 = 5;
    let b: f32 = 2.0;
    let c: bool = true;
}

ลองทำดู! 🎯

  1. ประกาศตัวแปรแต่ละชนิดและ print ออกมา
  2. สร้าง tuple ที่มี (ชื่อ, อายุ, ส่วนสูง) และแยกค่าออกมา
  3. สร้าง array ของวันในสัปดาห์

สรุป

ประเภทตัวอย่างคำอธิบาย
Integeri32, u64จำนวนเต็ม
Floatf32, f64ทศนิยม
Booleanbooltrue/false
Charactercharตัวอักษร Unicode
Tuple(i32, f64)ค่าหลายชนิด
Array[i32; 5]ค่าชนิดเดียว ขนาดคงที่

👉 ต่อไป: Constants & Shadowing