2 unstable releases
0.2.0 | Nov 22, 2023 |
---|---|
0.1.0 | Nov 22, 2023 |
#1358 in Rust patterns
4KB
unroll_range
unroll_range
is a Rust macro designed to facilitate the repetition of a block of code for each number within a specified range. This macro is particularly useful for tasks that require repetitive operations over a set of values, like iterations for testing, data processing, or generating output.
Usage
To use unroll_range
, include it in your Rust project. Call the macro with a range (inclusive or exclusive) and a closure that performs the operations you wish to repeat.
Parameters
$range
: A Rust range expression (either inclusive or exclusive) over which the block will be repeated.$block
: A closure that takes a single parameteri
and contains the code to execute for each iteration.
Example
Here's how you can use unroll_range
with an inclusive range:
unroll_range!(1..=3, |i| {
println!("Number: {}", i);
});
Comparison
crunchy
crunchy works fine, but adds a lot of boilerplate code, unnecessary if conditions.
repeated
repeated doesn't scope the block, so constants can't be used. It's also using procmacro, which seems to be a bit slower than a macro_rules macro.