# Lifetime ขั้นสูง (Rust) > Higher-ranked trait bounds (HRTB), lifetime subtyping, variance (covariant, contravariant, invariant), 'static vs 'a, NLL - 20 คำถามสัมภาษณ์ - Senior - [คำถามสัมภาษณ์: Rust](https://sharpskill.dev/th/technologies/rust/interview-questions.md) ## 1. Higher-Ranked Trait Bound (HRTB) ใน Rust คืออะไร? **คำตอบ** Higher-Ranked Trait Bound (HRTB) ช่วยให้สามารถระบุได้ว่าฟังก์ชันหรือ trait ต้องทำงานได้กับทุก lifetime ที่เป็นไปได้ ไม่ใช่เฉพาะ lifetime ใด lifetime หนึ่ง ไวยากรณ์ for<'a> บ่งบอกว่า trait bound ต้องเป็นจริงสำหรับ lifetime 'a ใด ๆ ที่ผู้เรียกเลือก คุณสมบัตินี้จำเป็นอย่างยิ่งสำหรับ closure และ callback ที่ต้องรับ reference ที่มี lifetime หลากหลาย ## 2. ความแตกต่างระหว่าง fn foo<'a>(f: impl Fn(&'a str)) และ fn foo(f: impl for<'a> Fn(&'a str)) คืออะไร? **คำตอบ** ด้วย fn foo<'a> lifetime 'a จะถูกกำหนดโดยผู้เรียกในขณะเรียกและคงเดิมตลอดการทำงาน ส่วน for<'a> closure ต้องทำงานได้กับ lifetime ใด ๆ ซึ่งทำให้สามารถเรียกได้หลายครั้งด้วย reference ที่มี lifetime ต่างกัน ความแตกต่างนี้สำคัญอย่างยิ่งสำหรับ API ที่เรียก closure หลายครั้งด้วย reference ชั่วคราวที่ต่างกัน ## 3. lifetime subtyping ใน Rust คืออะไร และ 'a: 'b หมายความว่าอย่างไร? **คำตอบ** lifetime subtyping สร้างความสัมพันธ์แบบ subtyping ระหว่าง lifetime สัญกรณ์ 'a: 'b หมายความว่า 'a มีอายุอย่างน้อยเท่ากับ 'b ดังนั้น 'a จึงเป็น subtype ของ 'b reference ที่มี lifetime ยาวกว่าสามารถใช้ในที่ที่คาดหวัง lifetime ที่สั้นกว่าได้ ความสัมพันธ์นี้เป็นแบบ covariant สำหรับ reference ที่ไม่เปลี่ยนแปลง ทำให้สามารถส่ง reference ที่มี lifetime ยาวกว่าในที่ที่ lifetime ที่สั้นกว่าก็เพียงพอ ## มีอีก 17 คำถาม - variance ใน Rust คืออะไร และส่งผลต่อ generic type อย่างไร? - ทำไม &mut T จึงเป็น invariant เมื่อเทียบกับ T ในขณะที่ &T เป็น covariant? สมัครฟรี: 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 - [Lifetimes](https://sharpskill.dev/th/technologies/rust/interview-questions/lifetimes.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 - [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/advanced-lifetimes