Expand description
Tiny bailing convenience macros.
Bailing is an error-handling pattern that takes the middle path between unwrap and ?:
- Compared to
unwrap: Bail willreturn,continue, orbreakinstead of panicking. - Compared to
?: Bail will log or ignore the error instead of propagating it.
The middle path avoids unwanted panics without the ergonomic challenges of propagating errors with ?.
§Getting started
This crate provides six macro variants:
Along with their tiny aliases:
r!,
rq!,
c!,
cq!,
b!, and
bq!.
The macros support Result, Option, and bool types out of the box.
Implement IntoResult to extend this to other types.
§Example
use tiny_bail::prelude::*;
// With `tiny_bail`:
fn increment_last(arr: &mut [i32]) {
*r!(arr.last_mut()) += 1;
}
// Without `tiny_bail`:
fn increment_last_manually(arr: &mut [i32]) {
if let Some(x) = arr.last_mut() {
*x += 1;
} else {
println!("Bailed at src/example.rs:34:18: `arr.last_mut()`");
return;
}
}Modules§
- Re-exported macros.
- Re-exported macros and tiny aliases.
Macros§
- Unwrap or break with a warning.
- Unwrap or break quietly.
- Unwrap or continue with a warning.
- Unwrap or continue quietly.
- Unwrap or return with a warning.
- Unwrap or return quietly.
Traits§
- An extension trait for separating success and failure values.