Crate unwrap_let

source ·
Expand description

This crate provides a single macro called unwrap_let!.

unwrap_let! lets you easily write a pattern-matching let assignment that panics when it fails. In essence, unwrap_let! is a let-else statement with less boilerplate.

unwrap_let! accepts a custom panic message similar to std::assert! macro.

Examples

let val = Some(123);

unwrap_let!(Some(x) = val);
// `x` is defined
assert_eq!(x, 123);
enum Integer {
    Signed(i64),
    Unsigned(u64),
}

fn negate(unchecked_num: Integer) -> Integer {
    unwrap_let!(Integer::Signed(num) = unchecked_num, "expected a signed integer");
    Integer::Signed(-num)
}

Macros

  • A Rust macro for quickly unwrapping a refutable pattern.