unwrap_or_else_error_handle 0.1.0

Function to handle errors in a way that prints a message and exits the program.
Documentation
  • Coverage
  • 50%
    1 out of 2 items documented0 out of 1 items with examples
  • Size
  • Source code size: 4.02 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 237.42 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • oblivisheee/unwrap_or_else_error_handle
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • oblivisheee

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"));
}