# คอลเลกชัน (Rust) > Vec, String, HashMap, HashSet, iterator, collect, ownership กับ collections - 20 คำถามสัมภาษณ์ - Junior - [คำถามสัมภาษณ์: Rust](https://sharpskill.dev/th/technologies/rust/interview-questions.md) ## 1. เมธอดใดที่ใช้เพิ่มสมาชิกไว้ที่ท้ายของ Vec? **คำตอบ** เมธอด push() เพิ่มสมาชิกไว้ที่ท้ายของเวกเตอร์ ทำให้ขนาดเพิ่มขึ้น 1 การดำเนินการนี้มีความซับซ้อนแบบเฉลี่ย O(1) เพราะเวกเตอร์อาจต้องจัดสรรหน่วยความจำใหม่หากเกินความจุ หากต้องการเพิ่มสมาชิกหลายตัว สามารถใช้ extend() หรือ append() ได้ตามความต้องการ ## 2. ความแตกต่างหลักระหว่าง String และ &str ใน Rust คืออะไร? **คำตอบ** String เป็นชนิดที่เป็นเจ้าของ (owned) ซึ่งเก็บไว้บน heap สามารถเปลี่ยนแปลงและปรับขนาดได้ ส่วน &str เป็นการอ้างอิงแบบไม่เปลี่ยนแปลงไปยังลำดับ UTF-8 ซึ่งมักเรียกว่า 'string slice' String เป็นเจ้าของข้อมูลของตัวเองและสามารถถูกแก้ไขได้ ในขณะที่ &str ยืมข้อมูลที่มีอยู่โดยไม่ได้เป็นเจ้าของ ความแตกต่างนี้เป็นพื้นฐานสำคัญสำหรับการจัดการหน่วยความจำใน Rust ## 3. จะสร้าง Vec ว่างใหม่ใน Rust ได้อย่างไร? **คำตอบ** Vec::new() สร้างเวกเตอร์ว่างโดยไม่มีการจัดสรรเริ่มต้น แมโคร vec![] ก็สามารถสร้างเวกเตอร์ว่างได้เช่นกัน แต่ Vec::new() เป็นเมธอดแบบชัดเจนมาตรฐาน ชนิดเจเนอริกสามารถอนุมานได้หรือระบุอย่างชัดเจนด้วยไวยากรณ์ turbofish Vec::::new() หรือด้วยการกำกับชนิด ## มีอีก 17 คำถาม - เมธอด get() คืนค่าอะไรเมื่อเรียกบน Vec? - ควรใช้โครงสร้างข้อมูลใดในการเชื่อมโยงคีย์กับค่าโดยมีการเข้าถึงเฉลี่ย O(1)? สมัครฟรี: 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/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 - [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/collections