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

Rust

8038 readers
5 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 3 years ago
MODERATORS
top 5 comments
sorted by: hot top controversial new old
[–] MoSal@programming.dev 0 points 4 months ago (1 children)
#![feature(macro_metavar_expr_concat)]
#![feature(macro_metavar_expr)]

macro_rules! gen_enums {
    ([$($name:ident)+]) => {
        gen_enums!(${count($name)}: [$($name)+]);
    };
    ($name:ident, $($tail:ident,)+) => {
        gen_enums!([$name $($tail)+]);
        gen_enums!($($tail,)+);
    };
    ($name:ident,) => {
        gen_enums!([$name]);
    };
    ($n:literal: [$($name:ident)+]) => {
        enum ${concat(Position, $n)} { $($name,)+ }
    };
}

gen_enums!(W, Z, Y, X,);
[–] SorteKanin@feddit.dk 0 points 4 months ago (1 children)

Not nearly as readable though

[–] MoSal@programming.dev 0 points 4 months 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.

[–] Vorpal@programming.dev 0 points 4 months ago

Uh, this blog says "introducing", but this is hardly new. I have seen this crate around for a while, and https://crates.io/crates/crabtime/versions corroborates that.

Is this an old blog? I can't find a date on it.

[–] jenesaisquoi@feddit.org 0 points 3 months ago

Doesn't support derive macros, unfortunately.