macro_rules! test11 {
($type:ident) => {
S1::$type(_)=>{/*mycode*/}
};
}
enum S1 {
A1(i32),
A2(i32)
}
fn main() {
let a=S1::A1(1);
match a {
test11!(A1);//这里怎么使用macro???
// test11!(A2)
_ => todo!(),
}
}
1
共 3 条评论, 1 页
评论区
写评论没记错宏是有限制的
比如宏的括号必须匹配,你不能生成一个只有左括号而需要人为补充右括号的宏。
一个解法可能是,在match之外包一层宏,这样你的宏只需要能够正常处理match就可以了。
https://zjp-cn.github.io/tlborm/syntax-extensions/expansion.html
我猜可能是不行的:https://stackoverflow.com/questions/44033221/how-to-write-macro-for-similar-matching-arms。
你可以用宏代替整个match,或者
macro => macro
,但是不能a => b
整体变成一个宏。这应该和match的AST展开模式有关,原理上可能就是不行的。