How to make quote for "" using syn::quote_spanned

I'd like to parse String::from("#value") into Rust codes using syn::quote_spanned or syn::quote

let value = String::from("Hello world");
quote! { String::from("#value") }

line:2 parses the #value directly, not to String::from("Hello World")

Is there any way to parse quotation mark in syn::quote or syn::quote_spanned?

Just doing String::from(#value) should work - the ToTokens implementation on strings outputs a string literal.

Oh, then what about syn::Ident?

let value: syn::Ident; // String::from("hello");
quote! { "#value" }

This will make quote like the real spanned quote?

This will be converted to a regular Rust identifier. To put its contents as a string you can call .to_string() on it beforehand.

Oh, it looks like there's a misunderstand.
I want this

let value: syn::Ident; // here, value is Hello
quote! { String::from("#value"); } // result should be String::from("Hello")

let value_str = value.to_string();
quote! { String::from(#value_str); }

thank you <3

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.