Declarative macros for common control-flow use cases such as break
, continue
, and return
.
Break from a loop if a given predicate evaluates to true.
```rust use flowcontrol::breakif;
breakif!(predicate); breakif!(predicate, label); ```
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 from a function if a given predicate evaluates to true. ```rust use flowcontrol::returnif;
returnif!(predicate); returnif!(predicate, value); ```
```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), ] ); ```
```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)], ); ```
```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), ] ); ```
```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), ] ); ```
```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]); ```
```rust use flowcontrol::returnif;
let getvalue = || { for n in 1..10 { returnif!(n == 5, "early return"); } return "return after loop"; };
asserteq!(getvalue(), "early return"); ```