In Rust, try, catch, and finally are not keywords. Instead, Rust uses the Result type, which can represent either a success or a failure. When a function returns a Result, you can use the match expression to check whether the result is successful (Ok) or a failure (Err).
Here is an example of how to use Result and match in a function that may produce an error:
fn divide(x: i32, y: i32) -> Result {
if y == 0 {
return Err("Division by zero error".to_string());
}
Ok(x / y)
}
fn main() {
let result = divide(10, 0);
match result {
Ok(value) => println!("Result: {}", value),
Err(error) => println!("Error: {}", error),
}
}
In the above example, the divide function takes two i32 arguments and returns a Result type. If the second argument is zero, the function returns an Err with a message. Otherwise, the function returns the result of dividing x by y as an Ok value.
In the main function, we call divide(10, 0) and store the result in the result variable. We then use the match expression to handle the Result. If the result is Ok, we print the value using println!. If the result is Err, we print the error message using println!.
If you want to perform some action regardless of whether an error occurs or not, you can use the ? operator or the match expression in a finally block-like function called finally from the ScopeGuard crate, like so:
use scopeguard::ScopeGuard;
let mut x = 0;
let scope_guard = ScopeGuard::defer(|| {
println!("Scope guard reached");
x = 1;
});
let result = divide(10, 0)?;
scope_guard.run_unwind(); // finally block
match result {
Ok(value) => println!("Result: {}", value),
Err(error) => println!("Error: {}", error),
}
println!("X value: {}", x); // X value: 1
In this example, we're using a scope guard to reach the finally block. The defer function creates a ScopeGuard object that executes the closure passed as an argument when it goes out of scope, which is when the finally block is reached. In this case, we set x to 1 inside the closure.
To use the finally block-like function provided by ScopeGuard, we call the run_unwind method on the ScopeGuard object wherever we want the block to be executed, which, in this case, is after the match expression in the example above.