dragostis
dragostis

Reputation: 2662

Can Rust macros create compile-time strings?

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.

Upvotes: 17

Views: 13939

Answers (1)

Shepmaster
Shepmaster

Reputation: 430681

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:

Upvotes: 25

Related Questions