Variables และ Types ใน Rust

Jan. 13, 2025 · boychawin

Rust เป็นภาษาที่โดดเด่นด้วยความปลอดภัยและความเร็ว โดยเฉพาะระบบการจัดการหน่วยความจำที่ไม่ต้องใช้ Garbage Collector ซึ่งช่วยลดข้อผิดพลาดในโปรแกรม และหัวใจของการเขียนโปรแกรมใน Rust คือ ตัวแปร (Variables) และ ประเภทข้อมูล (Types)

บทความนี้จะพามารู้จักเกี่ยวกับตัวแปรและประเภทข้อมูลใน Rust


ตัวแปรใน Rust: พื้นฐานและการใช้งาน

  1. ตัวแปร Immutable และ Mutable
    • ตัวแปรใน Rust มีค่าเริ่มต้นเป็น immutable (ไม่สามารถเปลี่ยนค่าได้) เพื่อช่วยลดข้อผิดพลาด
    • หากต้องการให้ตัวแปรสามารถเปลี่ยนค่าได้ ต้องระบุ mut
fn main() {
    let x = 10; // immutable
    let mut y = 20; // mutable
    y += 5; // เปลี่ยนค่าได้
    println!("x: {}, y: {}", x, y);
}

  1. Shadowing

    • Shadowing ช่วยให้เราประกาศตัวแปรชื่อเดียวกันซ้ำได้ โดยตัวแปรใหม่จะทับค่าตัวแปรเดิม
    • ใช้ได้ทั้งการเปลี่ยนประเภทและค่า
let x = 5;  
let x = x + 10;  
let x = "Hello, Rust!";  
println!("{}", x); // แสดงผล "Hello, Rust!"

  1. Type Annotation และ Type Inference

    • Rust รองรับการระบุประเภท (Type Annotation) และการเดาประเภท (Type Inference) โดยคอมไพล์เลอร์
let x: i32 = 42; // กำหนดประเภทเป็น i32  
let y = 3.14;    // คอมไพล์เลอร์เดาประเภทเป็น f64  

ประเภทข้อมูลใน Rust (Types)

ประเภทข้อมูลใน Rust แบ่งเป็น 3 กลุ่มใหญ่ ได้แก่ Scalar, Compound, และ Custom Types

  1. Scalar Types (ประเภทพื้นฐาน)

    • Integer: i8, i16, i32, i64, i128, u8, u16, u64, usize
    • Floating-point: f32, f64
    • Boolean: bool
    • Character: char
let age: i32 = 30;  
let price: f64 = 99.99;  
let is_available: bool = true;  
let letter: char = 'R';  

  1. Compound Types (ประเภทที่รวมหลายค่า) Tuple

    • เก็บค่าหลายค่าในประเภทที่ต่างกัน
let tup: (i32, f64, &str) = (42, 3.14, "Rust");
let (x, y, z) = tup; // แยกค่าจาก tuple

Array

  • เก็บค่าหลายค่าที่ประเภทเดียวกัน ขนาดคงที่
let arr: [i32; 4] = [1, 2, 3, 4];
let first = arr[0]; // เข้าถึงค่าตำแหน่งแรก

Slice

  • ใช้อ้างถึงส่วนหนึ่งของ Array หรือ String
let nums = [10, 20, 30, 40];
let slice = &nums[1..3]; // slice: [20, 30]

  1. Custom Types (ประเภทที่ผู้ใช้กำหนดเอง) Struct

    • ใช้สร้างโครงสร้างข้อมูล
struct Point {
    x: i32,
    y: i32,
}
let point = Point { x: 10, y: 20 };

Enum

  • ใช้กำหนดค่าที่เป็นไปได้หลายแบบ
enum Direction {
    Up,
    Down,
    Left,
    Right,
}
let move_dir = Direction::Up;

Option และ Result

  • Option ใช้จัดการค่าที่อาจไม่มี (nullable)
let some_value: Option<i32> = Some(42);
let no_value: Option<i32> = None;
  • Result ใช้จัดการข้อผิดพลาด
fn divide(a: f64, b: f64) -> Result<f64, String> {
    if b == 0.0 {
        Err(String::from("Cannot divide by zero"))
    } else {
        Ok(a / b)
    }
}

Ownership และ Borrowing: การจัดการหน่วยความจำ

Rust ใช้ระบบ Ownership และ Borrowing เพื่อจัดการหน่วยความจำโดยไม่ต้องใช้ Garbage Collector Reference (&) และ Mutable Reference (&mut)

let x = 10;  
let r = &x; // Reference (Borrowing)  
println!("r: {}", *r); // เข้าถึงค่าผ่าน reference  

let mut y = 20;  
let mr = &mut y;  
*mr += 10;  
println!("y: {}", y);

สรุป

Rust มีระบบตัวแปรและประเภทข้อมูลที่ยืดหยุ่นและปลอดภัย โดยออกแบบให้สอดคล้องกับการจัดการหน่วยความจำแบบไม่มีข้อผิดพลาด

Tags: rust , variables , types