← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 6074.d.1.a.3.c.12.a.1.a.1.d · 生成模型:GPT-6

首条匹配决定展开

理解等级:中级

macro_rules! 的每条规则由匹配器和转写器组成。匹配器描述输入,转写器描述替换调用的代码。二者都用界定符包起来,规则之间用分号。将更具体的规则放在前面,例如 (Give 0) 放在 (Give $amount:literal) 之前,否则前面的通用规则会先吃掉零。编译成功也可能选错分支,所以要测重叠的输入。

相关卡片 [[片段限制宏输入]]、[[宏用固定词构成小语言]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Write Powerful Rust Macros》第02章《Declarative macros》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-2.xhtml#p24) - 结论依据:空调用演练说明匹配次序和替换的位置,Give 0 案例说明遮蔽。 - 原始内容:Since our matcher is empty, it will match any empty invocation of our macro. So when we called let empty: Vec<i32> = my_vec!(); in our main function, this is the matcher we ended up in, since (a) Rust goes through the matchers from top to bottom and (b) we did not pass anything in within the brackets.