repetitive

Crates.iorepetitive
lib.rsrepetitive
version0.3.2
created_at2025-08-06 18:33:34.568678+00
updated_at2025-08-19 10:38:39.235801+00
descriptionMacro for generating repetitive code
homepage
repository
max_upload_size
id1784180
size209,135
(Noam2Stein)

documentation

README

Repetitive

Rust macro for writing repetitive code in a simpler, more readable, and more powerful way than declarative macros.

Features:

  • Repetition with @for
  • String/Identifier Concatenation with @["string" "string"]
  • Meta Variables with @let
  • Conditions with @if and @match

Repetition

Repetition 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;

String/Identifier Concatenation

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;

Meta Variables

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

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 {}

Operators And Methods

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;
Commit count: 0

cargo fmt