Rust nemá exceptions. Místo toho Result
Result a Option¶
use std::fs; fn read_config(path: &str) -> Result { fs::read_to_string(path) } fn find_user(id: u32) -> Option { users.iter().find(|u| u.id == id).cloned() } // ? operátor — propagace chyby fn process() -> Result<(), Box> { let config = fs::read_to_string(“config.toml”)?; let data: Config = toml::from_str(&config)?; Ok(()) }
Custom error types¶
use thiserror::Error;
[derive(Error, Debug)]¶
enum AppError {
[error(“User not found: {0}”)]¶
UserNotFound(u32),
[error(“Database error”)]¶
Database(#[from] sqlx::Error),
[error(“Invalid input: {0}”)]¶
Validation(String), }
Klíčový takeaway¶
Result pro chyby, Option pro nullable. ? operátor pro propagaci. thiserror pro custom error types.