[][src]Macro ward::ward

macro_rules! ward {
    ($o:expr) => { ... };
    ($o:expr, else $body:block) => { ... };
}

Returns the contents of a Option<T>'s Some(T), otherwise it returns early from the function.

Examples

// res is set to sut's contents
let sut = Some("test");
let res = ward!(sut);
assert_eq!("test", res);
// Because sut is None, the ward! returns early
let sut = None;
let res = ward!(sut);
unreachable!();
// Because sut is None, the ward!'s else branch will be run before it returns early
let sut = None;
let _res = ward!(sut, else {
    println!("This code will run!");
});
unreachable!();
// Unlike guard!, ward! can be used as an expression
let sut = Some("test");
print(ward!(sut));

fn print(text: &str) {
    println!("{}", text);
}