flow-control 0.1.1

Declarative macros for common flow-control use cases such as break, continue, and return.
Documentation
  • Coverage
  • 100%
    4 out of 4 items documented3 out of 3 items with examples
  • Size
  • Source code size: 10.68 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.19 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • nordzilla/flow-control
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • nordzilla

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.

use flow_control::break_if;

break_if!(predicate);
break_if!(predicate, label);

continue_if

Continue to the next iteration of a loop if a given predicate evaluates to true.

use flow_control::continue_if;

continue_if!(predicate);
continue_if!(predicate, label);

return_if

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

use flow_control::return_if;

return_if!(predicate);
return_if!(predicate, value);

Examples


break_if

Predicate only

use flow_control::break_if;

let mut v = Vec::new();
for outer_n in 1..3 {
    for inner_n in 1..5 {
        break_if!(inner_n == 3);
        v.push((outer_n, inner_n));
    }
}

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

Predicate and label

use flow_control::break_if;

let mut v = Vec::new();
'outer: for outer_n in 1..3 {
    for inner_n in 1..5 {
        break_if!(inner_n == 3, 'outer);
        v.push((outer_n, inner_n));
    }
}

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

continue_if

Predicate only

use flow_control::continue_if;

let mut v = Vec::new();
for outer_n in 1..3 {
    for inner_n in 1..5 {
        continue_if!(inner_n == 3);
        v.push((outer_n, inner_n));
    }
}

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

Predicate and label

use flow_control::continue_if;

let mut v = Vec::new();
'outer: for outer_n in 1..3 {
    for inner_n in 1..5 {
        continue_if!(inner_n == 3, 'outer);
        v.push((outer_n, inner_n));
    }
}

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

return_if

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");