知识卡片
拼接局部令牌片段
quote! 生成的令牌流可以只是一段 Rust 代码,例如 expect(…) 或一条尚未放到接收者之后的方法链。将小片段插值到外层 quote! 时,才需要保证组合结果符合目标语法。书中的字段生成器先复用相同的错误处理片段,再拼接字符串专用处理,减少重复。但若读者必须在多个令牌片段间来回追踪点号和接收者,保留完整分支可能更易读。
相关卡片 [[从字段生成构建器代码]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Testing a builder macro 查 AST 与组合令牌 · 查看原书依据
This is TokenStream all the way down, baby! We have said it before: quote offers some nice, flexible ways to compose output. So don’t think that individual functions need to return a complete, working piece of generated code.
参考来源
- 位置:《Write Powerful Rust Macros》第06章《Testing a builder macro》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-6.xhtml#p121)
- 结论依据:清单 6.25 预先生成 expect 片段,再按语法类型组装不同的方法链,最后插入目标字段表达式。
- 原始内容:This is TokenStream all the way down, baby! We have said it before: quote offers some nice, flexible ways to compose output. So don’t think that individual functions need to return a complete, working piece of generated code.