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屋!
查看全文