Safe unwrap replacements that log instead of panic.
.unwrap() and .expect() are useful during development but cause
production outages when invariants break. This crate provides drop-in
macro replacements that degrade gracefully: return a default value and
emit a structured log instead of panicking.
Quick Reference
| Before | After | For |
|---|---|---|
opt.unwrap() |
unwrap_or_warn!(opt, default, "ctx") |
Option<T> |
res.unwrap() |
result_or_warn!(res, default, "ctx") |
Result<T, E> |
s.parse::<T>().unwrap() |
parse_or_warn!(s, T, default, "ctx") |
String parsing |
opt.expect("msg") |
expect_or_error!(opt, default, "msg") |
Option soft-expect |
res.expect("msg") |
expect_result_or_error!(res, default, "msg") |
Result soft-expect |
All macros include file!() and line!() in the log output.
Example
use ;
// Option: return default on None
let name: = None;
let display = unwrap_or_warn!;
assert_eq!;
// Result: return default on Err
let val: = Err;
let count = result_or_warn!;
assert_eq!;
// Parse: return default on bad input
let port = parse_or_warn!;
assert_eq!;