flow_control

Declarative macros for common control-flow use cases such as break, continue, and return.


break_if

Break from a loop if a given predicate evaluates to true.

```rust use flowcontrol::breakif;

breakif!(predicate); breakif!(predicate, label); ```


continue_if

Continue to the next iteration of a loop if a given predicate evaluates to true. ```rust use flowcontrol::continueif;

continueif!(predicate); continueif!(predicate, label); ```


return_if

Return from a function if a given predicate evaluates to true. ```rust use flowcontrol::returnif;

returnif!(predicate); returnif!(predicate, value); ```

Examples


break_if

Predicate only

```rust use flowcontrol::breakif;

let mut v = Vec::new(); for outern in 1..3 { for innern in 1..5 { breakif!(innern == 3); v.push((outern, innern)); } }

assert_eq!( v, vec![ (1, 1), (1, 2), (2, 1), (2, 2), ] ); ```

Predicate and label

```rust use flowcontrol::breakif;

let mut v = Vec::new(); 'outer: for outern in 1..3 { for innern in 1..5 { breakif!(innern == 3, 'outer); v.push((outern, innern)); } }

assert_eq!( v, vec![(1, 1), (1, 2)], ); ```


continue_if

Predicate only

```rust use flowcontrol::continueif;

let mut v = Vec::new(); for outern in 1..3 { for innern in 1..5 { continueif!(innern == 3); v.push((outern, innern)); } }

assert_eq!( v, vec![ (1, 1), (1, 2), (1, 4), (2, 1), (2, 2), (2, 4), ] ); ```

Predicate and label

```rust use flowcontrol::continueif;

let mut v = Vec::new(); 'outer: for outern in 1..3 { for innern in 1..5 { continueif!(innern == 3, 'outer); v.push((outern, innern)); } }

assert_eq!( v, vec![ (1, 1), (1, 2), (2, 1), (2, 2), ] ); ```


return_if

Default return

```rust use flowcontrol::returnif;

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

```rust use flowcontrol::returnif;

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

asserteq!(getvalue(), "early return"); ```