6

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?

Jon Nimrod
  • 335
  • 1
  • 2
  • 12
  • You can't control the order of expansion. IIRC, somebody once suggested a syntax based on how many `!` you used, i.e. `!!x` gets resolved before `!f2`, but this idea did not find much support. I like your idea of a macro-based quine btw :) – Jan Hohenheim Mar 03 '19 at 14:37
  • I see, that's unfortunate! I managed to do it but without stringify!, so it's pretty unreadable unfortunately. – Jon Nimrod Mar 03 '19 at 17:22
  • 4
    @JonNimrod Please would you care to provide your workaround solution for this issue is documented and can be useful for others in the same situation? – Matias Cicero Oct 05 '20 at 18:18

0 Answers0