In *procedural* macros, is it possible to append to an identifier name?

Input:

pub struct Foo { ... }

output

pub enum Foo_Fields { ... }

here, x: proc_macro2::Ident has the value Foo, and I want to append a _Fields to it

2 Likes

Something like Ident::new(&format!("{x}_Fields"), x.span()).

3 Likes

The docs appears to agree with you:

/// Prints the identifier as a string that should be losslessly convertible back
/// into the same identifier.
impl Display for Ident {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        Display::fmt(&self.inner, f)
    }
}

If you are already using the quote crate, you can use its format_ident! macro that makes this a bit less verbose.

4 Likes