use quote::quote;
fn main() {
let name = "foo";
let res = quote!(#name bar);
println!("{:?}", res.to_string());
}
The above code prints "\"foo\" bar"
. Please try running it here on Rust Playground.
How to adjust it to make the single ident out of the variable part and constant suffix?
I want to use the quote!
returned value in a derive macro. How to get rid of the double quote characters?
Since you only need to quote bar, how about combining the usage of quote!
and format!
?
use quote::quote;
fn main() {
let name = "foo";
let res = format!("{} {}", name, quote!(bar));
println!("{:?}", res.to_string());
}
If you need the extra quote in the result:
use quote::quote;
fn main() {
let name = "foo";
let res = format!("\"{}{}\"", name, quote!(bar));
println!("{:?}", res.to_string());
}
Sorry. I think I didn't describe my question properly. I have update my question. Thanks for replying.
My case is a bit more complicated than what I described, but using
format!
first to concat and then put the result&str
inquote!
works for me. Thanks.