Programming Languages
Rust
Subjective
Oct 04, 2025
Explain Option and Result types in Rust.
Detailed Explanation
Option and Result for safe error handling:
• Option: Some(T) or None
• Result: Ok(T) or Err(E)
• No null pointer exceptions
• Explicit error handling
• Chainable with combinators
Example:
fn divide(a: f64, b: f64) -> Result {
if b == 0.0 {
Err("Division by zero".to_string())
} else {
Ok(a / b)
}
}
match divide(10.0, 2.0) {
Ok(result) => println!("Result: {}", result),
Err(e) => println!("Error: {}", e),
}
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts