expect-exit 0.1.0

Result.expected(): display an error message and exit without a panic.
Documentation

expect-exit: display an error message and exit without a panic

The expect-exit library defines the Expected trait and implements it for the standard Result enum so that a program can display an error message and exit with a non-zero exit code without invoking a Rust panic.

Functions

  • die(msg): display a message and exit with code 1
  • die_err(msg, err): display a message and the error itself and exit with code 1

Traits

  • Expected: declare two functions:

    • expected_(msg): display the message and exit if the expectation is not met
    • expected(msg): display the message and an appropriate error description and exit if the expectation is not met
  • an implementation of the Expected trait for the standard Result enum

Example

use expect_exit::Expected;

fn get_env_var(name: &str) -> String {
    env::var(name).expected_(&format!("{} not specified in the environment", name))
}