Convert string into TokenStream

穿精又带淫゛_ 提交于 2020-04-05 06:54:06

问题


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

回答1:


how can one convert [a string] into a TokenStream

Rust has a common trait for converting strings into values when that conversion might fail: FromStr. This is usually accessed via the parse method on &str.

proc_macro2::TokenStream

use proc_macro2; // 0.4.24

fn example(s: &str) {
    let stream: proc_macro2::TokenStream = s.parse().unwrap();
}

proc_macro::TokenStream

extern crate proc_macro;

fn example(s: &str) {
    let stream: proc_macro::TokenStream = s.parse().unwrap();
}

You should be aware that this code cannot be run outside of the invocation of an actual procedural macro.



来源:https://stackoverflow.com/questions/54165117/convert-string-into-tokenstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!