← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 6074.d.1.a.3.c.4.e.3.a.2.b · 生成模型:GPT-6

打印过程宏的语法树

理解等级:中级

用 cargo expand 可以观察宏最后发出了什么。若要看 syn 如何理解输入,在书中 syn 2.0.39 的 Cargo 依赖上启用 extra-traits,解析后用 eprintln!("{:#?}", &ast) 打印 DeriveInput 等语法节点的 Debug 表示。使用标准错误能避开部分调用方式对标准输出的捕获。

书中从打印结果看到 Fields::Named、首字段的 Visibility::Inherited 和 Ident 带有的 span。对照类型声明可分清类型有何字段,实际输入落到哪个变体,以及转换出错发生在哪一步。

相关卡片 [[从语法树提取命名字段]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Making fields public with attribute macros 打印编译期语法树 · 查看原书依据

      In previous chapters, we talked about development and debugging tools like cargo expand. In this chapter, we also did a lot of digging through source code, which helped us understand more of what data is available when syn has parsed your input. Another useful tool worth mentioning is—please don’t slam this book shut—printing to the console, because seeing the structure of your AST via types is useful, but printing it

    参考来源

    - 位置:《Write Powerful Rust Macros》第04章《Making fields public with attribute macros》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-4.xhtml#p104) - 结论依据:第 4.7 节对比读 AST 类型和打印实例,列出 extra-traits、stderr 写法和 Example 的缩略调试结果。 - 原始内容:In previous chapters, we talked about development and debugging tools like cargo expand. In this chapter, we also did a lot of digging through source code, which helped us understand more of what data is available when syn has parsed your input. Another useful tool worth mentioning is—please don’t slam this book shut—printing to the console, because seeing the structure of your AST via types is useful, but printing it