知识卡片
宏递归按语法终止
宏递归展开时,实参是令牌。把递归调用放进生成的 if,即使运行时会停止,展开时仍然要处理两支,5 - 1 只是变长的表达式。可让每次递归减少匹配到的元素数,直到匹配一个明确的基例。compose! 每次从序列取出一项,将剩余项传下去,单项规则结束。
相关卡片 [[相邻类型约束函数组合]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
- · 类型化的函数组合
原书出处 · 1 处
- Declarative macros 类型化的函数组合 · 查看原书依据
If our compose macro receives a single expression, a function, it returns that expression—a simple base case. If the macro is invoked with two or more arguments (note the + after $tail, which is for when there is more than a single expression in our tail), we need recursive magic. What we do is call our compose_two and pass in the two required arguments.
参考来源
- 位置:《Write Powerful Rust Macros》第02章《Declarative macros》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-2.xhtml#p135)
- 结论依据:计数器的展开记录反复追加减法,compose 用剩余参数缩短达到基例。
- 原始内容:If our compose macro receives a single expression, a function, it returns that expression—a simple base case. If the macro is invoked with two or more arguments (note the + after $tail, which is for when there is more than a single expression in our tail), we need recursive magic. What we do is call our compose_two and pass in the two required arguments.