Macro simple_error::require_with [−][src]
macro_rules! require_with { ($expr: expr, $str: expr) => { ... }; ($expr: expr, $fmt:expr, $($arg:tt)+) => { ... }; }
Helper macro for unwrapping Option
values while returning early with a
newly constructed SimpleError
if the value of the expression is None
.
Can only be used in functions that return Result<_, SimpleError>
.
Examples
use self::simple_error::SimpleError; fn require_block(maybe: Option<()>, s: &str) -> Result<(), SimpleError> { Ok(require_with!(maybe, s)) } // Above is equivalent to below. fn require_block_equivalent(maybe: Option<()>, s: &str) -> Result<(), SimpleError> { match maybe { Some(v) => Ok(v), None => { return Err(SimpleError::new(s)); }, } } // Use a format string fn require_block_format(maybe: Option<()>, s: &str) -> Result<(), SimpleError> { Ok(require_with!(maybe, "with {}", s)) }