Can a Rust macro create new identifiers?

I would like to create a couple of setter / getter functions where names are automatically generated based on a common component, but I could not find any example of macro rules generating a new name.

Is there a way to generate code like fn get_$iden() and SomeEnum::XX_GET_$enum_iden ?

+5
source share
1 answer

No, not rust 1.22.


If you can use nightly builds ...

Yes: concat_idents!(get_, $iden) and this will allow you to create a new identifier.

But no: the parser does not allow macro calls everywhere, so many of the places that you may have tried to make will not work. In such cases, you are sad to yourself. fn concat_idents!(get_, $iden)(…) { … } , for example, will not work.

+4
source

Source: https://habr.com/ru/post/1208780/


All Articles