0

I'm having issues finding how to use variables in parse_quote in the documentation.

I have a string variable that I need to insert into a turbofish statement I create with parse_quote.

Below are my attempts after reading some documentation and this question.

let my_var = String::from("hello");

let stmt = syn::parse_quote! { handle.add_class<#(my_var)::#(my_var)>(); };

This fails to compile with a warning about expecting other keywords.

I've also tried the following based on the documentation for parse_quote.

let my_var = quote!("hello");

let stmt = syn::parse_quote! { handle.add_class<#(my_var)::#(my_var)>(); };

This throws a similar error to the one above.

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
Mr.Smithyyy
  • 2,157
  • 12
  • 49
  • 95
  • What does `println!("{}", quote! { handle.add_class<#(my_var)::#(my_var)>(); });` ouput? Compare with the output when `my_var` is defined as: `let my_var = Ident::new("hello", Span::call_site());` – Peter Hall Aug 15 '22 at 23:45
  • Please reduce your code to something which can reproduce specific errors, and then please include those errors in your question. The code you have provided has a lot of problems and doesn't look like it's the same code that you have been running (e.g. `String.from` instead of `String::from`... – Peter Hall Aug 15 '22 at 23:47
  • It should be without parentheses and proper turbofish (`handle.add_class::<#my_var::#my_var>();`). – Chayim Friedman Aug 16 '22 at 03:37

0 Answers0