Can macro compose names from its arguments?


#1

Hi.

I want to make a macro that generates a function name from its arguments.

macro_rule! example {
    ($i:ident) => (
        fn example_$i() {
            // some code
        }
    );
}

example!(AA); // => I want it to generate function example_AA

Is it possible?


#2

There’s concat_idents! but that notes its own limitations. AFAICS it doesn’t work for declaring a function.

Edit: see http://www.ncameron.org/blog/untitledconcat_idents-and-macros-in-ident-position/


#3

Yes, I tried concat_idents, that can generate identifiers already defined, but cannot define…

Thank you.


#4

concat_idents! is probably not being stabilized.

For inspirations to tackle this problem using modules, see here: https://github.com/mikkyang/rust-blas/pull/12