return_if

Macro return_if 

Source
macro_rules! return_if {
    ($predicate:expr $(,)?) => { ... };
    ($predicate:expr, $ret:expr $(,)?) => { ... };
}
Expand description

return from a function if a given predicate evaluates to true.

Supports optionally providing a value to return.

§Usage

return_if!(predicate)

return_if!(predicate, value)

§Examples

§Default return
use flow_control::return_if;

let mut v = Vec::new();
(|| {
    for n in 1..10 {
        return_if!(n == 5);
        v.push(n)
    }
})();

assert_eq!(v, vec![1, 2, 3, 4]);
§Return a specified value
use flow_control::return_if;

let get_value = || {
    for n in 1..10 {
        return_if!(n == 5, "early return");
    }
    return "return after loop";
};

assert_eq!(get_value(), "early return");