How to make a macro expand before stringifying it in Rust?

552 Views Asked by At

I'm trying to write a quine in Rust using only macros. In order to do that, I'm embedding the main function into macro f1, and trying to embed a literal representation of f1 in f2 with stringify!.

Here is my code so far:

macro_rules!f1{()=>(fn main(){println!("macro_rules!{}\nmacro_rules!f2{{($x:expr)=>(stringify!($x))}}\nf1!();",f2!(f1));})}
macro_rules!f2{($x:expr)=>(stringify!($x))}
f1!();

The output is, unsurprisingly:

macro_rules!f1
macro_rules!f2{($x:expr)=>(stringify!($x))}
f1!();

What I need is for f1 to expand before being stringified in order for the program to be quine. How can I do that?

0

There are 0 best solutions below