# Lifetimes (Rust) > Lifetime annotation, กฎ elision, lifetime 'static, lifetime ของ struct, lifetime bounds - 20 คำถามสัมภาษณ์ - Mid-Level - [คำถามสัมภาษณ์: Rust](https://sharpskill.dev/th/technologies/rust/interview-questions.md) ## 1. จุดประสงค์หลักของ lifetime ใน Rust คืออะไร? **คำตอบ** Lifetime ช่วยให้ compiler ตรวจสอบว่า reference ทั้งหมดใช้งานได้ตลอดช่วงการใช้งาน โดยรับประกันว่า reference จะไม่มีอายุยืนกว่าข้อมูลที่มันอ้างถึง จึงป้องกัน dangling reference ได้ borrow checker ใช้ lifetime เพื่อตรวจสอบความปลอดภัยของหน่วยความจำในขณะคอมไพล์ โดยไม่มี overhead ขณะรันไทม์ ## 2. ใช้ไวยากรณ์ใดในการระบุ lifetime บน reference? **คำตอบ** Lifetime ถูกระบุด้วยเครื่องหมายอะพอสทรอฟีตามด้วยชื่อ โดยทั่วไปเป็นตัวอักษรพิมพ์เล็กเช่น 'a ไวยากรณ์ &'a T หมายถึง reference ไปยัง T ที่มี lifetime 'a ตามธรรมเนียมจะใช้ 'a, 'b, 'c สำหรับ lifetime แบบ generic แต่สามารถใช้ตัวระบุที่ถูกต้องใด ๆ หลังเครื่องหมายอะพอสทรอฟีได้ ## 3. Lifetime 'static หมายความว่าอย่างไรใน Rust? **คำตอบ** Lifetime 'static แสดงว่า reference สามารถมีอายุได้ตลอดช่วงเวลาทั้งหมดของโปรแกรม string literal เช่น "hello" จะมี lifetime 'static โดยอัตโนมัติเพราะถูกฝังอยู่ใน binary ค่าคงที่ระดับ global และค่าที่สร้างด้วย Box::leak ก็มี lifetime นี้เช่นกัน หมายเหตุ: 'static ไม่ได้หมายความว่าค่ามีอยู่ตั้งแต่เริ่มโปรแกรม แต่หมายความว่ามันสามารถมีอายุอยู่จนจบได้ ## มีอีก 17 คำถาม - Compiler ของ Rust ใช้กฎ lifetime elision โดยอัตโนมัติกี่ข้อ? - ทำไมฟังก์ชัน fn longest(x: &str, y: &str) -> &str จึงคอมไพล์ไม่ได้? สมัครฟรี: https://sharpskill.dev/th/login ## หัวข้อสัมภาษณ์ Rust อื่นๆ - [พื้นฐาน Rust](https://sharpskill.dev/th/technologies/rust/interview-questions/rust-basics.md): 25 คำถาม, Junior - [Ownership & Borrowing](https://sharpskill.dev/th/technologies/rust/interview-questions/ownership-borrowing.md): 22 คำถาม, Junior - [Structs & Enums](https://sharpskill.dev/th/technologies/rust/interview-questions/structs-enums.md): 20 คำถาม, Junior - [การจัดการข้อผิดพลาด](https://sharpskill.dev/th/technologies/rust/interview-questions/error-handling.md): 18 คำถาม, Junior - [คอลเลกชัน](https://sharpskill.dev/th/technologies/rust/interview-questions/collections.md): 20 คำถาม, Junior - [โมดูลและแพ็กเกจ](https://sharpskill.dev/th/technologies/rust/interview-questions/modules-packages.md): 18 คำถาม, Junior - [Traits](https://sharpskill.dev/th/technologies/rust/interview-questions/traits.md): 22 คำถาม, Mid-Level - [Generics](https://sharpskill.dev/th/technologies/rust/interview-questions/generics.md): 20 คำถาม, Mid-Level - [Iterators & Closures](https://sharpskill.dev/th/technologies/rust/interview-questions/iterators-closures.md): 22 คำถาม, Mid-Level - [Smart Pointers](https://sharpskill.dev/th/technologies/rust/interview-questions/smart-pointers.md): 22 คำถาม, Mid-Level - [Concurrency Basics](https://sharpskill.dev/th/technologies/rust/interview-questions/concurrency-basics.md): 20 คำถาม, Mid-Level - [async/await](https://sharpskill.dev/th/technologies/rust/interview-questions/async-await.md): 22 คำถาม, Mid-Level - [Testing](https://sharpskill.dev/th/technologies/rust/interview-questions/testing.md): 18 คำถาม, Mid-Level - [Cargo & Ecosystem](https://sharpskill.dev/th/technologies/rust/interview-questions/cargo-ecosystem.md): 18 คำถาม, Mid-Level - [Pattern Matching](https://sharpskill.dev/th/technologies/rust/interview-questions/pattern-matching.md): 18 คำถาม, Mid-Level - [มาโคร](https://sharpskill.dev/th/technologies/rust/interview-questions/macros.md): 20 คำถาม, Mid-Level - [Serde และการ Serialization](https://sharpskill.dev/th/technologies/rust/interview-questions/serde-serialization.md): 20 คำถาม, Mid-Level - [Unsafe Rust](https://sharpskill.dev/th/technologies/rust/interview-questions/unsafe-rust.md): 20 คำถาม, Senior - [Trait ขั้นสูง](https://sharpskill.dev/th/technologies/rust/interview-questions/advanced-traits.md): 22 คำถาม, Senior - [Lifetime ขั้นสูง](https://sharpskill.dev/th/technologies/rust/interview-questions/advanced-lifetimes.md): 20 คำถาม, Senior - [Type System](https://sharpskill.dev/th/technologies/rust/interview-questions/type-system.md): 20 คำถาม, Senior - [Tokio & Async I/O](https://sharpskill.dev/th/technologies/rust/interview-questions/tokio-async.md): 22 คำถาม, Senior - [การเพิ่มประสิทธิภาพ](https://sharpskill.dev/th/technologies/rust/interview-questions/performance-optimization.md): 20 คำถาม, Senior - [Memory Management](https://sharpskill.dev/th/technologies/rust/interview-questions/memory-management.md): 20 คำถาม, Senior - [Web Frameworks](https://sharpskill.dev/th/technologies/rust/interview-questions/web-frameworks.md): 22 คำถาม, Senior - [Database Integration](https://sharpskill.dev/th/technologies/rust/interview-questions/database-integration.md): 20 คำถาม, Senior - [Design Pattern ของ Rust](https://sharpskill.dev/th/technologies/rust/interview-questions/design-patterns.md): 20 คำถาม, Senior --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/th/technologies/rust/interview-questions/lifetimes