知识卡片
属性宏可以改写所附代码项
属性宏可接收所附函数等代码项的token并生成替代结构,例如在函数体前后加入调用日志。它不同于仅作标记的普通属性,可能改变实际执行代码。使用者需了解生成行为,特别是返回、错误与副作用是否仍保持接口契约。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 5. Tooling Attribute macros · 查看原书依据
Attribute macros Attribute macros are invoked by placing them before some item in the program, and the parsed tokens for that item are the input to the macro. The macro can again emit arbitrary tokens as output, but the output is typically some transformation of the input. For example, an attribute macro can be used to wrap the body of a function: #[log_invo
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第5章《Attribute macros》(spine9;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch05.html;u145-ch05-013)。
- 结论依据:原文在“Attribute macros”附近的定义、例子或代码支撑本卡核心说明:属性宏可接收所附函数等代码项的token并生成替代结构,例如在函数体前后加入调用日志。边界按已读前提解释;示例未执行。
- 原始内容:Attribute macros Attribute macros are invoked by placing them before some item in the program, and the parsed tokens for that item are the input to the macro. The macro can again emit arbitrary tokens as output, but the output is typically some transformation of the input. For example, an attribute macro can be used to wrap the body of a function: #[log_invo