Repetitive
Rust macro for repetitive code generation which is easier to use than declarative macros,
or a combination of macro crates like paste and seq_macro.
For Loop
repetitive! {
@for name in ['StructA, 'StructB, 'StructC] {
struct @name;
}
}
Generates:
struct StructA;
struct StructB;
struct StructC;
Concat
repetitive! {
@for letter in ['A, 'B, 'C] {
struct @['Struct letter];
}
}
Generates:
struct StructA;
struct StructB;
struct StructC;
Expressions
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,
}
}
}
}
Generates:
struct Vec2 {
x: f32,
y: f32,
}
struct Vec3 {
x: f32,
y: f32,
z: f32,
}
struct Vec4 {
x: f32,
y: f32,
z: f32,
w: f32,
}
If Statement
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 {}
Expression Methods
Expression methods have full auto-completion and documentation.
repetitive! {
@for N in 2..=4 {
@let VecN = @['Vec N];
@let components = ['x, 'y, 'z, 'w][0..N];
println!("{} has: {}", @str[VecN], @(components.concat_string()));
}
}
Generates:
println!("{} has: {}", "Vec2", "xy");
println!("{} has: {}", "Vec3", "xyz");
println!("{} has: {}", "Vec4", "xyzw");