Given a string (str
), how can one convert that into a TokenStream
in Rust?
I've tried using the quote!
macro.
let str = "4";
let tokens = quote! { let num = #str; }; // #str is a str not i32
The goal here is to generate tokens for some unknown string of code.
let thing = "4";
let tokens = quote! { let thing = #thing }; // i32
or
let thing = ""4"";
let tokens = quote! { let thing = #thing }; // str
str
is not ani32
, which is a complet different question. – Olivaolivaceouslet str = 4 as i32
and watch it turn into ani32
literal inquote!
. – LandgrabberTokenStream
from code you already know the structure of, and where does the additional data on types come from (even in your example, the type ofthing
could very well be anu8
, or ani16
, or anything else that4
conforms to). What are you actually trying to parse? – LandgrabberTokenStream
comes from. Do you meanproc_macro::TokenStream
orproc_macro2::TokenStream
, or some other one? – Stevenage