Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Rust macros create compile-time strings?

Tags:

macros

rust

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.

like image 924
dragostis Avatar asked Feb 19 '16 19:02

dragostis


1 Answers

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:

  • How to create a static string at compile time
like image 100
Shepmaster Avatar answered Oct 15 '22 14:10

Shepmaster