Smart Pointers
ใน Rust, Smart Pointers ไม่ใช่แค่ตัวเก็บ Address แบบ Pointer ใน C/C++ แต่มันคือ Struct ที่มีความสามารถพิเศษ (เช่น มี Metadata, จัดการ Memory อัตโนมัติ)
ลองนึกภาพว่า Smart Pointers เป็นเหมือน “กล่องอัจฉริยะ” ที่รู้ว่าต้องจัดการกับของข้างในยังไง ไม่ใช่แค่กล่องธรรมดา!
1. Box<T> - Heap Allocation
Section titled “1. Box<T> - Heap Allocation”1.1 Basic Usage
Section titled “1.1 Basic Usage”fn main() { // ============================================ // Box<T> = allocate T บน heap // ============================================ let b = Box::new(5); println!("b = {}", b);
// ============================================ // Dereference ด้วย * // ============================================ let x = *b; println!("x = {}", x);
// b ถูก drop และ memory ถูก free อัตโนมัติ}เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google