17

Macro variables are escaped in Rust macros by default. Is there any way to have them not escaped?

macro_rules! some {
    ( $var:expr ) => ( "$var" );
}

some!(1) // returns "$var", not "1"

This is useful for concatenating compile-time strings and such.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
dragostis
  • 2,574
  • 2
  • 20
  • 39

1 Answers1

25

It sounds like you want stringify!:

macro_rules! some {
    ( $var:expr ) => ( stringify!($var) );
}

fn main() {
    let s = some!(1);
    println!("{}", s);
}

And you will probably want concat! too.

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366