| Crates.io | unwrap_or_else_error_handle |
| lib.rs | unwrap_or_else_error_handle |
| version | 0.1.0 |
| created_at | 2025-05-25 17:17:11.675168+00 |
| updated_at | 2025-05-25 17:17:11.675168+00 |
| description | Function to handle errors in a way that prints a message and exits the program. |
| homepage | |
| repository | https://github.com/oblivisheee/unwrap_or_else_error_handle |
| max_upload_size | |
| id | 1688551 |
| size | 4,025 |
A simple Rust utility to handle errors by printing a custom message and exiting the program.
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.
fn main() {
let result = Err("file not found");
let _ = result.unwrap_or_else(handle_error("Failed to open file"));
}