Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Quiz: บทที่ 9 - Error Handling

คำถาม 9.1

Result<T, E> มี variants อะไร?

A. Some และ None
B. Ok และ Err
C. Success และ Failure
D. Value และ Error

ดูเฉลย

B. Ok และ Err

enum Result<T, E> {
    Ok(T),
    Err(E),
}

คำถาม 9.2

? operator ทำอะไร?

A. Return None
B. Unwrap หรือ return error
C. Panic
D. Print error

ดูเฉลย

B. Unwrap หรือ return error

fn read_file() -> Result<String, Error> {
    let content = std::fs::read_to_string("file.txt")?;
    Ok(content)
}

ถ้า error จะ return Err ออกจาก function ทันที


คำถาม 9.3

panic! ใช้เมื่อไหร่?

A. ทุก error
B. Unrecoverable errors
C. Network errors
D. User input errors

ดูเฉลย

B. Unrecoverable errors

panic! ใช้เมื่อโปรแกรมไม่สามารถดำเนินต่อได้อย่างปลอดภัย


คำถาม 9.4

unwrap() ต่างจาก expect() อย่างไร?

A. ไม่ต่าง
B. expect มี custom message
C. unwrap ไม่ panic
D. expect return Option

ดูเฉลย

B. expect มี custom message

let x = some_option.unwrap();       // generic message
let x = some_option.expect("msg"); // custom message
```text

</details>

---

# คำถาม 9.5

`unwrap_or()` ทำอะไร?

A. Panic ถ้า None  
B. Return ค่า default ถ้า None  
C. Return Result  
D. แปลงเป็น Option

<details>
<summary>ดูเฉลย</summary>

**B. Return ค่า default ถ้า None**

```rust,ignore
let x = some_option.unwrap_or(0); // ใช้ 0 ถ้า None

👉 Quiz บทที่ 10