Convert string into TokenStream

前端 未结 1 1993
天命终不由人
天命终不由人 2020-12-07 04:33

Given a string (str), how can one convert that into a TokenStream in Rust?

I\'ve tried using the quote! macro.



        
相关标签:
1条回答
  • 2020-12-07 05:15

    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.

    0 讨论(0)
提交回复
热议问题