Joining paths and idents in macros

I have a few different use cases for joining paths and identifiers in macros:

  • Accept a path to a mod and repeated idents and join them to make types without repeating the mod.
  • Accept a path to an enum type and repeated idents and join them to either construct instances of the enum, or to use in match arms, without having to repeat the name of the enum.
  • etc

I've managed to make a few of these use cases work, but an intuitive, multi-purpose "just join these tokens with ::" mechanism is eluding me.

Intuitively, I'd be able to say $my_mod:path, $my_type:ident and then construct $my_mod::$my_type(my, args), but that doesn't work. If I do $my_enum_type:ty, $my_enum_case:ident, I can construct enums with <$my_enum_type>::$my_enum_case(my, args), but I can't write a match arm in the same way.

Is there some syntax here I'm missing? So far, I don't actually understand the use case for a path designator at all, given that I can't seem to combine paths in any interesting way.

Check out the paste crate.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.