【发布时间】:2016-02-21 12:44:28
【问题描述】:
直接传递给宏的类型模式匹配您所期望的方式,但如果它们通过另一个宏作为ty 传递,它们将停止匹配:
macro_rules! mrtype {
( bool ) => ("b");
( i32 ) => ("i");
( f64 ) => ("f");
( &str ) => ("z");
( $_t:ty ) => ("o");
}
macro_rules! around {
( $t:ty ) => (mrtype!($t));
}
fn main() {
println!("{}{}{}", mrtype!(i32), around!(i32), around!(&str));
}
这将打印 ioo 而不是 iiz。
传递tts 而不是tys 是可行的,但如果您有&str,则需要2 个tts,这会使一切变得不必要地复杂。
【问题讨论】: