แบบฝึกหัด: บทที่ 9 - Error Handling
แบบฝึกหัดที่ 1: Result
เขียน function parse_number ที่:
- รับ
&str - Return
Result<i32, String>
ดูเฉลย
fn parse_number(s: &str) -> Result<i32, String> {
s.parse::<i32>()
.map_err(|_| format!("'{}' is not a valid number", s))
}
fn main() {
match parse_number("42") {
Ok(n) => println!("Parsed: {}", n),
Err(e) => println!("Error: {}", e),
}
match parse_number("abc") {
Ok(n) => println!("Parsed: {}", n),
Err(e) => println!("Error: {}", e),
}
}
แบบฝึกหัดที่ 2: ? Operator
แปลงโค้ดนี้ให้ใช้ ?:
fn read_username() -> Result<String, std::io::Error> {
let file = match std::fs::read_to_string("username.txt") {
Ok(content) => content,
Err(e) => return Err(e),
};
Ok(file.trim().to_string())
}
ดูเฉลย
fn read_username() -> Result<String, std::io::Error> {
let content = std::fs::read_to_string("username.txt")?;
Ok(content.trim().to_string())
}
แบบฝึกหัดที่ 3: unwrap_or
แก้ไขโค้ดให้ใช้ unwrap_or แทน match:
let value = match some_option {
Some(v) => v,
None => 0,
};
ดูเฉลย
let value = some_option.unwrap_or(0);
// หรือใช้ unwrap_or_default() ถ้า type implement Default
let value = some_option.unwrap_or_default();
แบบฝึกหัดที่ 4: Custom Error
สร้าง custom error DivisionError สำหรับการหารที่ผิดพลาด
ดูเฉลย
use std::fmt;
#[derive(Debug)]
struct DivisionError {
message: String,
}
impl fmt::Display for DivisionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for DivisionError {}
fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
if b == 0 {
Err(DivisionError {
message: String::from("Cannot divide by zero"),
})
} else {
Ok(a / b)
}
}
fn main() {
match divide(10, 0) {
Ok(result) => println!("Result: {}", result),
Err(e) => println!("Error: {}", e),
}
}
แบบฝึกหัดที่ 5: Chaining Results
เขียน function ที่:
- Parse string เป็น number
- คูณด้วย 2
- Return Result
ดูเฉลย
fn parse_and_double(s: &str) -> Result<i32, std::num::ParseIntError> {
s.parse::<i32>().map(|n| n * 2)
}
fn main() {
println!("{:?}", parse_and_double("5")); // Ok(10)
println!("{:?}", parse_and_double("abc")); // Err(...)
}
👉 บทที่ 10