How to determine if a string is a valid rust identifier?

How to determine if a string is a valid rust identifier?

I feel like there might be a function in the standard library to do this somewhere?

Identifiers are described here in terms of the Unicode XID_start and XID_continue properties. You could implement the rules described there very easily using the unicode_xid crate.

For a less manual approach, you could use syn::parse_str::<syn::Ident> as recommended by the proc-macro2 documentation.

But I have to ask: why do you want to do this?

1 Like

Yeah it's debatable. I think the argument is that I can give a better error message than the compiler would otherwise when it gets used in the code gen.

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.