dtolnay / quote

Rust quasi-quoting
Apache License 2.0
1.32k stars 90 forks source link

format operator such as `+` or `-` into an identifier #220

Closed IbrahimFadel closed 2 years ago

IbrahimFadel commented 2 years ago

I'm trying to use macros inside of quote, and i need the identifier i pass to my macro to be an operator

let ident = format_ident!("-");
quote! {
  foo!(#ident)
}

This panics with '"-" is not a valid Ident'. It makes sense since it's not a valid identifier, but is there some other way i could accomplish this?

I can avoid the panic by just doing let ident = format!("-");, but then quote generates foo!("-") which is not what i want. I want foo!(-).

I could work around this by changing my macro to accept foo!("-"), but i would rather not have to do that.

If quote is intentionally built this way, then it's okay. But otherwise, it would be nice to have this as a feature.

dtolnay commented 2 years ago

Hi @IbrahimFadel, sorry that no one was able to provide guidance here. If this is still an issue, you could try taking this question to any of the resources shown in https://www.rust-lang.org/community.