Thanks to visit codestin.com
Credit goes to lib.rs

2 stable releases

Uses new Rust 2024

new 1.0.1 May 19, 2026

#1316 in Rust patterns

MIT license

14KB
136 lines

Package reunroll

A #![no_std] Rust macro crate for compile-time loop unrolling via tail recursion.

The unroll! macro expands a fixed-count loop body inline, with the loop index available as a usize constant in each iteration. Because the count is a literal digit token known at compile time, the compiler sees no branches and no counter variable, only straight-line code.

[dependencies]
reunroll = "1.0"

Syntax

unroll!(COUNT, i, { /* body, may reference i */ });
Token Description
COUNT 0x000x40, number of iterations
i Identifier bound to the current index (usize)
block Code block executed for each index

Example

use reunroll::unroll;

let mut sum = 0usize;
unroll!(0x08, i, {
    sum += i;
});
assert_eq!(sum, 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7);

How it works

Each unroll!(N, ...) arm first calls unroll!(N-1, ...) (tail recursion in the macro expander), then emits one copy of the body with i bound to N-1. The recursion bottoms out at unroll!(0x00, ...), which expands to nothing. The Rust compiler then sees fully unrolled, straight-line code with no runtime overhead.

License

MIT

No runtime deps