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, yet optionally
unwinding the stack so that various objects may perform some clean-up actions.
Functions
exit_unwind(code): unwind the stack and exit with the specified codeexit(msg): display a message and exit with code 1exit_perror(msg, err): display a message and the error itself and exit with code 1die(msg): the same asexit(), but do not unwind the stackdie_perror(msg, err): the same asexit_perror(), but do not unwind the stack
Traits
-
Expected: declare two methods:expect_or_exit(msg): display the message and exit if the expectation is not metexpect_or_die(msg): the same asexpect_or_exit(), but do not unwind the stack
-
ExpectedWithError: declare two additional methods:expect_or_exit_perror(msg): display the message and an appropriate error description and exit if the expectation is not metexpect_or_die_perror(msg): the same asexpect_or_exit_perror(), but do not unwind the stack
-
an implementation of the
Expectedtrait for the standardOptionenum -
an implementation of the
ExpectedandExpectedWithErrortraits for the standardResultenum
Example
use expect_exit::Expected;
fn get_env_var(name: &str) -> String {
env::var(name).expect_or_exit(&format!("{} not specified in the environment", name))
}