Ownership
JavaScript จัดการ memory ให้คุณ — Rust ไม่ทำแบบนั้น
หัวข้อที่มีชื่อว่า “JavaScript จัดการ memory ให้คุณ — Rust ไม่ทำแบบนั้น”ใน JavaScript และ TypeScript garbage collector คอย track ทุก heap value (string, array, object) ให้เอง คุณสร้าง value ได้อย่างอิสระ แล้ว GC จะดูให้ว่าเมื่อไหร่ไม่มีใครใช้แล้วค่อยคืน memory ไม่ต้องเรียก free() เอง และไม่ต้องกังวลเรื่อง use-after-free
Rust ไม่มี garbage collector แต่มี ownership: ระบบกฎ compile-time ที่ให้ทุก value มี owner แค่คนเดียว เมื่อ owner หลุด scope value จะถูกคืน memory อัตโนมัติ — ไม่ต้องการ GC
การโอน ownership (Move)
หัวข้อที่มีชื่อว่า “การโอน ownership (Move)”ใน TypeScript การ assign object ไปให้ตัวแปรใหม่สร้าง reference ที่สอง ตัวแปรทั้งสองชี้ไปที่ data เดียวกัน ใน Rust การ assign heap value move ownership: ตัวแปรเดิมจะใช้ไม่ได้แล้ว
// TypeScript — สอง reference, object เดียวกันconst a = { name: "Alice" };const b = a; // b ชี้ไปที่ object เดียวกันconsole.log(a.name); // "Alice" — a ยังใช้ได้console.log(b.name); // "Alice" — ทั้งคู่ทำงาน
// GC จะคืน object เมื่อ a และ b ทั้งคู่หลุด scope// คุณควบคุมเวลาไม่ได้fn main() { // Rust — String เป็น heap value let a = String::from("Alice"); let b = a; // ownership MOVES จาก a ไป b // println!("{}", a); // error[E0382]: use of moved value: `a` println!("{}", b); // ใช้ได้แค่ b
// เมื่อ b หลุด scope ปลาย main() // Rust คืน String data อัตโนมัติ ไม่ต้องการ GC}Ownership ผ่าน function
หัวข้อที่มีชื่อว่า “Ownership ผ่าน function”การส่ง heap value เข้า function จะ move ค่านั้นเข้าไปด้วย ฝั่ง caller จึงใช้ค่าเดิมต่อไม่ได้ เว้นแต่ function จะ return กลับมาให้
// TypeScript — ส่ง object ไม่ได้ move ownershipfunction greet(name: string) { console.log(`Hello, ${name}`);}
const s = "world";greet(s);console.log(s); // ยังใช้ได้ — JS ส่ง reference สำหรับ objectfn greet(name: String) { // name เป็น owner ที่นี่ println!("Hello, {}", name);} // name ถูก drop ที่นี่
fn main() { let s = String::from("world"); greet(s); // ownership move เข้าไปใน greet() // println!("{}", s); // error[E0382]: use of moved value: `s`}ลองเขียนเอง
หัวข้อที่มีชื่อว่า “ลองเขียนเอง”fn takes_ownership(s: String) { println!("got: {}", s);} // s dropped here
fn makes_copy(n: i32) { println!("copy: {}", n);} // n dropped here, but i32 is Copy so the caller's copy is unaffected
fn main() { let s = String::from("hello"); takes_ownership(s); // println!("{}", s); // would be a compile error — s was moved
let x = 5; makes_copy(x); println!("x is still valid: {}", x); // fine — i32 is Copy}Compiling…