29
you are viewing a single comment's thread
view the rest of the comments
[-] MoSal@programming.dev 2 points 3 days ago* (last edited 3 days ago)

(slightly edited from initial version)

#![feature(macro_metavar_expr_concat)]
#![feature(macro_metavar_expr)]

macro_rules! gen_enums {
    ([$($name:ident)+]) => {
        gen_enums!(${count($name)}: [$($name)+]);
    };
    ($n:literal: [$($name:ident)+]) => {
        enum ${concat(Position, $n)} { $($name,)+ }
    };
    ($name:ident, $($tail:ident,)*) => {
        gen_enums!([$name $($tail)*]);
        gen_enums!($($tail,)*);
    };
    () => {};
}
[-] SorteKanin@feddit.dk 2 points 3 days ago

Not nearly as readable though

[-] MoSal@programming.dev 4 points 3 days ago

It was just a quick solution showing how to do it with one macro and zero dependencies, utilizing the power of meta variables. It probably can be made nicer.

Side Note: It's not directly relevant/needed here, but this also shows that maybe a theoretical $reverse{} meta variable can be useful with repeated patterns, to in this case generate the variants in the expected order.

this post was submitted on 28 Jan 2026
29 points (100.0% liked)

Rust

7716 readers
44 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 2 years ago
MODERATORS