#macro #unwrapping #options #continue #standard #logic #methods

unwrap-macros

Convenience macro for unwrapping result and option

1 unstable release

0.1.0 Mar 1, 2025

#1471 in Rust patterns

Download history 113/week @ 2025-02-24 40/week @ 2025-03-03

153 downloads per month

MIT license

4KB

unwrap-macros

This tiny library provides unwrapping macros in situation where the typical unwrapping methods for Result and Option in the standard library comes short and the alternative is too verbose. Specifically when you want to have the unwrap_or_else logic but need to continue or return.


lib.rs:

This tiny library provides unwrapping macros in situation where the typical unwrapping methods for Result and Option in the standard library comes short and the alternative is too verbose. Specifically when you want to have the unwrap_or_else logic but need to continue or return.

Usage

This will log the error with eprintln! and skips the iteration.

// with Result
let some_stuff = vec![
    Ok(1),
    Ok(2),
    Err(MyError::First),
    Ok(4),
    Err(MyError::Second),
];
for thing in some_stuff {
    let val = unwrap_or_else!(Result, thing, continue);
    println!("{}", val);
}

You can also supply a closure-like argument:

for thing in some_stuff {
    let val = unwrap_or_else!(Result, thing, |e| {
        // some code
        eprintln!("Custom message for error: {e}");
        continue,
    });
    println!("{}", val);
}
// with Option
let some_stuff = vec![
    Some(1),
    Some(2),
    None,
    Some(4),
    None,
];
for thing in some_stuff {
    let val = unwrap_or_else!(Option, thing, continue);
    println!("{}", val);
}

This will log "No value" when None is matched.

for thing in some_stuff {
    let val = unwrap_or_else!(Option, thing, "No value", continue);
    println!("{}", val);
}

No runtime deps