Result<T, E> — handle success or failure.
Basic usage:
1fn divide(a: f64, b: f64) -> Result<f64, String> {2 if b == 0.0 {3 Err(String::from("Division by zero"))4 } else {5 Ok(a / b)6 }7}89match divide(10.0, 2.0) {10 Ok(result) => println!("Result: {}", result),11 Err(e) => println!("Error: {}", e),12}
Error propagation with ?:
1fn read_config() -> Result<Config, Box<dyn std::error::Error>> {2 let content = std::fs::read_to_string("config.toml")?;3 let config: Config = toml::from_str(&content)?;4 Ok(config)5}
Key: Use ? for propagation, match for handling.