知识卡片
打印过程宏的语法树
用 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