← 返回起始笔记位置

知识卡片

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

宏入口归一默认值

理解等级:中级

宏可以用不同规则识别实参数量,再归一为同一调用。问候宏只有名字时补入默认问候语,有两个实参时原样转给函数。生成访问器的样板也可归一:只有类型名时转调带显式 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.