Error Handling in RUST
Rust has built-in support for handling errors using the Result type. Functions that can fail return a Result that either contains the successful result or an error. Here's an example:
rust
fn divide(x: i32, y: i32) -> Result<i32, String> {
if y == 0 {
Err(String::from("division by zero"))
} else {
Ok(x / y)
}
}
fn main() {
match divide(10, 5) {
Ok(result) => println!("Result: {}", result),
Err(error) => println!("Error: {}", error),
}
}
No comments:
Post a Comment