I am implementing a function-like procedural macro which takes a single string literal as an argument, but I don't know how to get the value of the string literal.
If I print the variable, it shows a bunch of fields, which includes both the type and the value. They are clearly there, somewhere. How do I get them?
extern crate proc_macro; use proc_macro::{TokenStream,TokenTree}; #[proc_macro] pub fn my_macro(input: TokenStream) -> TokenStream { let input: Vec<TokenTree> = input.into_iter().collect(); let literal = match &input.get(0) { Some(TokenTree::Literal(literal)) => literal, _ => panic!() }; // can't do anything with "literal" // println!("{:?}", literal.lit.symbol); says "unknown field" format!("{:?}", format!("{:?}", literal)).parse().unwrap() } #![feature(proc_macro_hygiene)] extern crate macros; fn main() { let value = macros::my_macro!("hahaha"); println!("it is {}", value); // prints "it is Literal { lit: Lit { kind: Str, symbol: "hahaha", suffix: None }, span: Span { lo: BytePos(100), hi: BytePos(108), ctxt: #0 } }" }