| Crates.io | repetitive |
| lib.rs | repetitive |
| version | 0.3.2 |
| created_at | 2025-08-06 18:33:34.568678+00 |
| updated_at | 2025-08-19 10:38:39.235801+00 |
| description | Macro for generating repetitive code |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1784180 |
| size | 209,135 |
Rust macro for writing repetitive code in a simpler, more readable, and more powerful way than declarative macros.
Features:
@for@["string" "string"]@let@if and @matchRepetition is done with the @for keyword.
The body is repeated for each value in the given list.
repetitive! {
@for name in ['StructA, 'StructB, 'StructC] {
struct @name;
}
}
Expands to:
struct StructA;
struct StructB;
struct StructC;
Concatenation is done using @["a" "b"] syntax.
repetitive! {
@for letter in ['A, 'B, 'C] {
#[doc = @str["Letter " letter "!"]]
struct @['Struct letter];
}
}
Expands to:
/// Letter A!
struct StructA;
/// Letter B!
struct StructB;
/// Letter C!
struct StructC;
Use @let to define variables in the macro context to improve readability.
repetitive! {
@for N in 2..=4 {
@let VecN = @['Vec N];
@let components = ['x, 'y, 'z, 'w][0..N];
struct @VecN {
@for c in @components {
@c: f32,
}
}
}
}
Expands to:
struct Vec2 {
x: f32,
y: f32,
}
struct Vec3 {
x: f32,
y: f32,
z: f32,
}
struct Vec4 {
x: f32,
y: f32,
z: f32,
w: f32,
}
Conditions are done using @if.
repetitive! {
trait Even {}
trait Odd {}
@for i in 0..4 {
@let NumberI = @['Number i];
struct @NumberI;
@if i % 2 == 0 {
impl Even for @NumberI {}
} else {
impl Odd for @NumberI {}
}
}
}
Generates:
trait Even {}
trait Odd {}
struct Number0;
struct Number1;
struct Number2;
struct Number3;
impl Even for Number0 {}
impl Odd for Number1 {}
impl Even for Number2 {}
impl Odd for Number3 {}
Macro context expressions support operators and methods.
repetitive! {
@for N in [1, 2, 3] {
@let crazy = (N * 2).min(5);
const _: u32 = @crazy;
}
}
Expands to:
const _: u32 = 2;
const _: u32 = 4;
const _: u32 = 5;