← 返回起始笔记位置

知识卡片

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

按调用控制代码生成

理解等级:中级

宏参数可以决定某一次展开是否添加可选实现。书中的属性宏从 exclude = "from" 得到 exclude_from 布尔值,输出生成器由此选择真正的 From 实现或空 token 流,再插入最终 quote!。这使同一个 crate 的不同调用可以生成不同的 API。

使用空 token 可以让可选部分沿用同一个引用位置。参数输入仍应定义好合法键、合法值以及多项能否共存。原书先展示只能给出 path 或 exclude 的简化版本,附录再处理两个属性一起出现的情况。

相关卡片 [[特性应叠加功能]]、[[用标点管理多个配置项]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Write Powerful Rust Macros》第10章《Macros and the outside world》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-10.xhtml#p78) - 结论依据:原文明确说明布尔值由解析结果传到输出构造处,决定生成 From 或返回空 token 流。 - 原始内容:The other changes are located in struct_output.rs. Two functions (generate_ inserts_for_from and generate_from_method) help us generate the proper From. Meanwhile, generate_annotation_struct receives a Boolean from lib.rs, where we pull it from ConfigInput.