知识卡片
重复捕获按层展开
$($x:expr),+ 表示用逗号分隔的至少一个表达式。* 表示零到多个,? 表示零或一个。模板里的 $(v.push($x);)+ 让每个捕获到的表达式产生一条语句。转写器要在与绑定对应的重复层级使用变量,不能在外层直接把整个序列当成一次 push 的实参。若 API 接受末尾逗号,可另外加 $(,)?。
相关卡片 [[片段限制宏输入]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
- · 重复及类型检查
原书出处 · 1 处
- Declarative macros 重复及类型检查 · 查看原书依据
Within our matcher, we can see that $x:expr is now wrapped with $( ),+. That tells Rust to accept “one or more expressions, separated by a comma.” As a programmer, it will not surprise you to hear that in addition to a +, you can use a * for zero or more occurrences and ? for zero or one.
参考来源
- 位置:《Write Powerful Rust Macros》第02章《Declarative macros》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-2.xhtml#p56)
- 结论依据:原文对照匹配-转写重复并给出尾逗号与重复深度错误。
- 原始内容:Within our matcher, we can see that $x:expr is now wrapped with $( ),+. That tells Rust to accept “one or more expressions, separated by a comma.” As a programmer, it will not surprise you to hear that in addition to a +, you can use a * for zero or more occurrences and ? for zero or one.