知识卡片
宏入口归一默认值
宏可以用不同规则识别实参数量,再归一为同一调用。问候宏只有名字时补入默认问候语,有两个实参时原样转给函数。生成访问器的样板也可归一:只有类型名时转调带显式 String 的规则,后一个规则生成唯一的 impl 模板。这样逻辑只维护一份,Rust 函数保留固定签名。
相关卡片 [[宏递归按语法终止]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Declarative macros newtype 与宏分担样板 · 查看原书依据
You may have noticed that there is a lot of overlap between our two code paths. Why not have one of our cases use the other? Since the String pair is a special case of the other one (which can handle any type), it makes sense for our special case to use the generic internally.
参考来源
- 位置:《Write Powerful Rust Macros》第02章《Declarative macros》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-2.xhtml#p99)
- 结论依据:问候和访问器示例分别展示多个规则调用同一函数或转调同一模板。
- 原始内容:You may have noticed that there is a lot of overlap between our two code paths. Why not have one of our cases use the other? Since the String pair is a special case of the other one (which can handle any type), it makes sense for our special case to use the generic internally.