How to Check If a Result-Returning Function Succeeded in Rust

I'm looking for a more concise way to check if a function that returns a Result succeeded or not. Currently, I'm using a combination of map_err and ok to check for errors and ignore the successful result. However, this approach can be verbose and hard to read.

Is there an idiomatic way in Rust to check if a Result-returning function failed or not without resorting to match statements? I'm open to any suggestions that can make the code more concise and easier to read.

...
    myexe::testing_func(&worker)
        .map_err(|e| {
            error(format!("Failed to blabla: {}", e));
            std::process::exit(1);
        })
        .ok();
...

While I'd personally do it the same way as you do it, I like the simplicity of this answer on SO:

if let Err(e) = myexe::testing_func(&worker) {
    error(format!("Failed to blabla: {}", e));
    std::process::exit(1);
}
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.