Quiz: บทที่ 8 - Collections
คำถาม 8.1
Vec::new() สร้างอะไร?
A. Fixed array
B. Empty vector
C. Linked list
D. HashMap
ดูเฉลย
B. Empty vector
let v: Vec<i32> = Vec::new();
// หรือ
let v = vec![1, 2, 3];
```text
</details>
---
# คำถาม 8.2
`String` ต่างจาก `&str` อย่างไร?
A. ไม่ต่าง
B. `String` เป็น owned, `&str` เป็น borrowed
C. `String` เร็วกว่า
D. `&str` ใหญ่กว่า
<details>
<summary>ดูเฉลย</summary>
**B. `String` เป็น owned, `&str` เป็น borrowed**
- `String` = เก็บบน heap, เป็นเจ้าของ, แก้ไขได้
- `&str` = reference ไป string slice
</details>
---
# คำถาม 8.3
`HashMap::get()` return อะไร?
A. ค่าตรงๆ
B. `Option<&V>`
C. `Result<V, E>`
D. `&V`
<details>
<summary>ดูเฉลย</summary>
**B. `Option<&V>`**
```rust,ignore
let value = map.get("key"); // Option<&V>
```text
Return `Some(&value)` ถ้าพบ, `None` ถ้าไม่พบ
</details>
---
# คำถาม 8.4
`vec.push(value)` ทำอะไร?
A. เพิ่มตัวแรก
B. เพิ่มตัวสุดท้าย
C. ลบตัวแรก
D. ลบตัวสุดท้าย
<details>
<summary>ดูเฉลย</summary>
**B. เพิ่มตัวสุดท้าย**
```rust,ignore
let mut v = vec![1, 2, 3];
v.push(4); // [1, 2, 3, 4]
```text
</details>
---
# คำถาม 8.5
`entry().or_insert()` ใช้ทำอะไร?
A. ลบค่าถ้ามี
B. แทรกค่าถ้ายังไม่มี
C. อัปเดตค่าเสมอ
D. ดึงค่าออกมา
<details>
<summary>ดูเฉลย</summary>
**B. แทรกค่าถ้ายังไม่มี**
```rust,ignore
let count = map.entry("key").or_insert(0);
*count += 1;