# Design Pattern ของ Rust (Rust) > Builder pattern, newtype, RAII, typestate, pattern การจัดการ error, pattern interior mutability - 20 คำถามสัมภาษณ์ - Senior - [คำถามสัมภาษณ์: Rust](https://sharpskill.dev/th/technologies/rust/interview-questions.md) ## 1. ข้อได้เปรียบหลักของ Builder pattern ใน Rust เมื่อเทียบกับ constructor ที่มีพารามิเตอร์จำนวนมากคืออะไร? **คำตอบ** Builder pattern ช่วยให้สร้างออบเจกต์ได้อย่างอ่านง่ายและยืดหยุ่นโดยการเชน method แบบ fluent ใน Rust มันหลีกเลี่ยงข้อผิดพลาดที่เกี่ยวข้องกับลำดับของพารามิเตอร์ อนุญาตให้มีค่าเริ่มต้น และทำให้โค้ดอธิบายตัวเองได้ ต่างจาก constructor ที่มี argument ตามตำแหน่งจำนวนมาก Builder ทำให้แต่ละ field ชัดเจนผ่านชื่อ method ของมัน ## 2. ใน Newtype pattern การห่อ primitive type ไว้ใน tuple struct แก้ปัญหาอะไร? **คำตอบ** Newtype pattern สร้าง type ที่แตกต่างในระดับ type system ป้องกันความสับสนระหว่างค่าที่ต่างกันในเชิงความหมายแต่มี type พื้นฐานเดียวกัน ตัวอย่างเช่น UserId(u64) และ ProductId(u64) เข้ากันไม่ได้ที่ compile-time หลีกเลี่ยง bug ที่จะส่ง ID สินค้าไปในที่ที่คาดหวัง ID ผู้ใช้ pattern นี้มีต้นทุน runtime เป็นศูนย์เนื่องจากการรับประกันการแทนค่าหน่วยความจำที่เหมือนกัน ## 3. RAII pattern รับประกันการปล่อย resource ใน Rust อย่างไร? **คำตอบ** RAII (Resource Acquisition Is Initialization) ผูกวงจรชีวิตของ resource เข้ากับ scope ของตัวแปร ใน Rust trait Drop จะถูกเรียกโดยอัตโนมัติเมื่อค่าออกจาก scope รับประกันการปล่อย resource แบบกำหนดได้โดยไม่ต้องมี garbage collector pattern นี้เป็นพื้นฐานสำหรับไฟล์ การเชื่อมต่อเครือข่าย lock และ resource ใด ๆ ที่ต้องการการ cleanup อย่างชัดเจน ## มีอีก 17 คำถาม - หลักการของ Typestate pattern ในการเข้ารหัสสถานะของออบเจกต์ใน type system คืออะไร? - รูปแบบการจัดการ error แบบใดที่แนะนำสำหรับการส่งต่อ error พร้อมบริบทเพิ่มเติม? สมัครฟรี: 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 - [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 --- 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/design-patterns