Can Rust macros create compile-time strings?

2019-06-20 05:57发布

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.

标签: macros rust
1条回答
唯我独甜
2楼-- · 2019-06-20 06:21

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:

查看更多
登录 后发表回答