macrosrust

How to prefix/suffix identifiers within a macro?


When using a macro that defines a function, is it possible to add a prefix or a suffix to the function name?

macro_rules! my_test {
    ($id:ident, $arg:expr) => {
        #[test]
        fn $id() {
            my_test_impl(stringify!($id), $arg);
        }
    }
}

For example, fn my_test_$id() {

I'm defining tests using an identifier which may begin with numbers, and I would like to use a common prefix.


Solution

  • [...] is it possible to add a prefix to the function?

    No. Really, really no. Super totally not at all even in the slightest.

    I would like to have use a common prefix.

    Put them all in a mod instead.