知识卡片
用标点管理多个配置项
解析多项 DSL 参数时,可以按重复结构来选解析方法。固定的 name : type 字段可依次消费标识符、冒号和类型。若输入允许连续放入多个自带关键字的属性,例如 path = "a.yaml" exclude = "from",解析器需要循环到输入为空,并为每个选项保存独立值。
若语法明确用分隔符连接同类条目,可让 Punctuated<Item, Sep> 承担列表的重复及分隔检查。它的第二个参数可换成所选 token 类型。本书演示语法 compose!(add_one ! add_one ! stringify) 使用 Token!(!)。重复同一个选项时该覆盖还是报错,属于另一条规则,需要按语法约定决定。
相关卡片 [[分隔语法简化属性解析]]、[[按调用控制代码生成]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Exercise solutions 第 10 章练习:多属性与正向 feature · 查看原书依据
Our starting point is the project before we added documentation. Everything we need to change is in input.rs. Instead of an if-else, we use while to loop as long as the stream is not empty.
参考来源
- 位置:《Write Powerful Rust Macros》附录 A《Exercise solutions》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/appendix-a.xhtml#p177)
- 结论依据:第 10 章练习将单一 if-else 改成读至流空,并提出分隔形式可以交给 Punctuated。第 4、5 章练习展示固定字段与可更换分隔符。
- 原始内容:Our starting point is the project before we added documentation. Everything we need to change is in input.rs. Instead of an if-else, we use while to loop as long as the stream is not empty.