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 1die_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 metexpected(msg): display the message and an appropriate error description and exit if the expectation is not met
-
an implementation of the
Expectedtrait for the standardResultenum
Example
use expect_exit::Expected;
fn get_env_var(name: &str) -> String {
env::var(name).expected_(&format!("{} not specified in the environment", name))
}