#exit #expect #panic #terminate

bin+lib expect-exit

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

10 releases

Uses new Rust 2021

0.5.1 Aug 21, 2022
0.4.3 Jun 4, 2022
0.4.1 Sep 1, 2021
0.4.0 Jun 22, 2021
0.2.0 Nov 13, 2020

#233 in Rust patterns

Download history 78/week @ 2022-06-05 11/week @ 2022-06-12 16/week @ 2022-06-19 9/week @ 2022-06-26 16/week @ 2022-07-03 25/week @ 2022-07-10 25/week @ 2022-07-17 28/week @ 2022-07-24 21/week @ 2022-07-31 24/week @ 2022-08-07 36/week @ 2022-08-14 50/week @ 2022-08-21 10/week @ 2022-08-28 41/week @ 2022-09-04 28/week @ 2022-09-11 17/week @ 2022-09-18

100 downloads per month
Used in 4 crates

BSD-2-Clause

48KB
1K SLoC

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

The expect-exit library defines the Expected, ExpectedWithError, and ExpectedResult traits and implements them for the standard Result and Option types as appropriate. This allows a program to 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.

The methods with an _e suffix append an appropriate error message to the supplied one. The methods with a _ suffix allow the caller to specify an already-constructed message instead of a function that returns it.

Example

use expect_exit::{Expected, ExpectedResult};

{
    env::var(name).or_exit(|| format!("{} not specified in the environment", name))

    fs::read_to_string(path).or_exit_e(|| format!("Could not read {:?}", path))

    tx.send(result).await.or_exit_e_("Could not tell the main thread");

    let config = parse().expect_result_("Could not parse the config")?;
    Ok(config.value + 1)
}

For the crate's change history, see the NEWS.md file in the source distribution.

Author: Peter Pentchev <roam@ringlet.net>

No runtime deps