Rust宏可以创建编译时字符串吗? [英] Can Rust macros create compile-time strings?
本文介绍了Rust宏可以创建编译时字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
默认情况下,宏变量在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.
推荐答案
听起来像您要 stringify!
:
It sounds like you want stringify!
:
macro_rules! some {
( $var:expr ) => ( stringify!($var) );
}
fn main() {
let s = some!(1);
println!("{}", s);
}
您可能还需要 concat!
.
And you will probably want concat!
too.
另请参阅:
这篇关于Rust宏可以创建编译时字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文