When using a macro that defines a function, is it possible to add a prefix to the function?
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.
No. Really, really no. Super totally not at all even in the slightest.
Put them all in a
mod
instead.As mentioned, you should use submodules for this, but remember that macros can create submodules, submodules can be nested allowing their names to overlap, submodules can provide
impl
s, and thetests
submodule is not magic.I once submitted a pull request that avoids numerous "boiler plate names" by refactoring the code using these tricks, although the
#[no_mangle]
exports make it harder.Currently this is not supported in stable.
However there is a feature in nightly called
concat_idents
:See
Update: it seems there aren't near-term plans to add this into stable releases, see issue.