unwrap_or_else_error_handle

Crates.iounwrap_or_else_error_handle
lib.rsunwrap_or_else_error_handle
version0.1.0
created_at2025-05-25 17:17:11.675168+00
updated_at2025-05-25 17:17:11.675168+00
descriptionFunction to handle errors in a way that prints a message and exits the program.
homepage
repositoryhttps://github.com/oblivisheee/unwrap_or_else_error_handle
max_upload_size
id1688551
size4,025
Oleg Pogoraev (oblivisheee)

documentation

README

unwrap_or_else_error_handle

A simple Rust utility to handle errors by printing a custom message and exiting the program.

Usage

Add the function to your project:

use unwrap_or_else_error_handle::handle_error;

Suppose you have a function that returns a Result:

fn might_fail(success: bool) -> Result<&'static str, &'static str> {
    if success {
        Ok("All good!")
    } else {
        Err("Something went wrong")
    }
}

You can handle errors like this:

let value = might_fail(false)
    .unwrap_or_else(handle_error("An error occurred"));

If an error occurs, this will print:

An error occurred: Something went wrong

and exit the program with status code 1.

Example

fn main() {
    let result = Err("file not found");
    let _ = result.unwrap_or_else(handle_error("Failed to open file"));
}
Commit count: 3

cargo fmt