← 返回刚才阅读的位置

知识卡片

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

按语法种类生成原项

理解等级:中级

同一个过程宏若支持多种 Rust 项,宜在识别 AST 变体的分支里生成相应形态。具名字段写在一对花括号内,元组结构体写成括号里的类型列表,枚举则重放变体。书中的 #[public] 给枚举本身加 pub,并将其变体写回输出。

重建原项时要显式保留还需要的外围语法。附录示范将 AST 上的 attrs 迭代插回项之前,#(#attributes)* 内不加逗号。逗号对字段或变体列表有意义,连续的属性用它分隔会导致编译错误。

相关卡片 [[修改节点保留外围语法]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Exercise solutions 第 4 章练习:保留原项结构 · 查看原书依据

      Instead of making the match output the fields, we now make it generate all our code, because while we are in the match, we know exactly what kind of struct (named or unnamed) we are dealing with. That makes it easier to generate the right kind of structure. The specific retrieval and transformation logic has been moved to separate functions.

    参考来源

    - 位置:《Write Powerful Rust Macros》附录 A《Exercise solutions》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/appendix-a.xhtml#p46) - 结论依据:原文解释在 match 时已知具体类型,因此可以直接产生对应项,并在相邻解答补上原始属性的保留方法。 - 原始内容:Instead of making the match output the fields, we now make it generate all our code, because while we are in the match, we know exactly what kind of struct (named or unnamed) we are dealing with. That makes it easier to generate the right kind of structure. The specific retrieval and transformation logic has been moved to separate functions.