知识卡片
片段限制宏输入
$x:expr 把满足表达式语法的输入绑定为元变量 x,转写器用 $x 重用它。ident、ty、literal 可限制所需输入。一个 tt 捕获一个令牌树,例如单个令牌或界定符分组,需求较宽时常用重复来捕获序列。具体片段能更早暴露错误,设计分隔符也要服从片段后继限制,例如 expr 可后接 ,、; 或 =>。
相关卡片 [[重复捕获按层展开]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
- · 片段与块表达式
原书出处 · 1 处
- Declarative macros 片段与块表达式 · 查看原书依据
The most powerful of these metavariables is tt (TokenTree), and it will accept almost anything you pass to it. That’s a powerful option. But its comprehensiveness can also be a downside. For simpler types, Rust can catch mistakes, like when you pass in a literal while the macro only matches an ident.
参考来源
- 位置:《Write Powerful Rust Macros》第02章《Declarative macros》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-2.xhtml#p34)
- 结论依据:原文比较 tt 的灵活性和更明确片段的早期反馈,组合宏展示 expr 后继约束。
- 原始内容:The most powerful of these metavariables is tt (TokenTree), and it will accept almost anything you pass to it. That’s a powerful option. But its comprehensiveness can also be a downside. For simpler types, Rust can catch mistakes, like when you pass in a literal while the macro only matches an ident.