← 返回刚才阅读的位置

知识卡片

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

字段名与生成方法名分离

理解等级:中级

字段名确定数据的存取位置,生成方法名是调用者使用的接口。处理重命名时,先用 Field.attrs 中的路径找到 rename,解析出可选标识符。有配置时用选定名字生成 setter,缺省则用原字段名。书中 #[rename("tops_of")] 让调用者写 tops_of(..),方法内仍写入 roots_of。测试可以同时断言生成方法可用以及值进入原字段。

相关卡片 [[派生辅助属性配置展开]]、[[属性形状决定解析方法]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Write Powerful Rust Macros》第08章《Builder with attributes》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-8.xhtml#p41) - 结论依据:清单 8.1 和 8.5 同时验证调用的 setter 名称已变,而赋值目标及成品仍使用原来字段名。 - 原始内容:Once we are done changing the attribute into a custom method name, we use if let to get the value from the Option if it is present. In every other case, we default to our previous implementation, where the field and method names are the same.