知识卡片
尾随标点独立可选
逗号分隔的宏列表中,分隔逗号与末尾可选逗号分别用不同的重复表达式处理。($($x:expr),+ $(,)?) 中,第一个重复匹配一个或多个逗号分隔的表达式,第二个重复允许末尾出现零或一个逗号。这样 my_vec!(1, 2) 与 my_vec!(1, 2,) 共用一个展开分支。空输入可交给 () 分支。
相关卡片 [[转发规则共享宏实现]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Exercise solutions 第 2 章练习:匹配与组合 · 查看原书依据
Alternatively, you could add another rule for matching a trailing comma. In that case, you probably want to define the transcriber body only once and make one of the rules for multiple elements use the other one internally.
参考来源
- 位置:《Write Powerful Rust Macros》附录 A《Exercise solutions》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/appendix-a.xhtml#p11)
- 结论依据:练习清单给出 $(,)? 解法,段落说明若另写规则,也应让两个多元素分支共用转写实现。
- 原始内容:Alternatively, you could add another rule for matching a trailing comma. In that case, you probably want to define the transcriber body only once and make one of the rules for multiple elements use the other one internally.