Přeskočit na obsah
_CORE
AI & Agentic Systems Core Informační Systémy Cloud & Platform Engineering Data Platforma & Integrace Security & Compliance QA, Testing & Observability IoT, Automatizace & Robotika Mobile & Digital Banky & Finance Pojišťovnictví Veřejná správa Obrana & Bezpečnost Zdravotnictví Energetika & Utility Telco & Média Průmysl & Výroba Logistika & E-commerce Retail & Loyalty
Reference Technologie Blog Know-how Nástroje
O nás Spolupráce Kariéra
Pojďme to probrat

Rust Error Handling

20. 08. 2022 1 min čtení intermediate

Rust nemá exceptions. Místo toho Result a Option — explicitní, type-safe error handling.

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.

rusterror handlingprogramming
Sdílet:

CORE SYSTEMS tým

Stavíme core systémy a AI agenty, které drží provoz. 15 let zkušeností s enterprise IT.