【发布时间】:2020-03-11 13:17:17
【问题描述】:
我(天真地)试过这个,但它不会在屏幕上打印任何东西:
macro_rules! foo {
($suffix:tt, $arg:expr) => {
concat!("foo", $suffix, "(", $arg, ")");
};
}
fn foo_i32(x: i32) {
println!("i32 {}", x);
}
fn foo_bool(x: bool) {
println!("bool {}", x);
}
fn main() {
foo!("bool", true);
foo!("i32", 1);
}
【问题讨论】:
-
Here's a more principled way of writing that using the type system to advantage. 当然,如果需要,您仍然可以定义
foo!宏,但使用特征比天真地将标识符粘合在一起更可靠。
标签: rust macros rust-macros rust-decl-macros