← 返回起始笔记位置

知识卡片

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

宏和依赖各有作用域

理解等级:高级

普通 macro_rules! 可按文本作用域在定义之后出现。可以显式用 pub(crate) use 在 crate 内按路径提供名字,旧代码常见 #[macro_use]。#[macro_export] 将宏从 crate 根导出。展开中使用的函数仍受路径和可见性限制。导出宏要引用定义 crate 的项目时,可用 $crate::路径 清楚指定归属,同时满足目标项目的可见性。lazy_static! 用保留的 @MAKE TY 和 @TAIL 区分内部步骤。

相关卡片 [[局部绑定具有卫生性]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Write Powerful Rust Macros》第02章《Declarative macros》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-2.xhtml#p72) - 结论依据:问候例子在调用者处使用辅助函数,lazy_static 内部用 $crate 指定依赖归属。 - 原始内容:Note that we had to make our base_greeting_fn function public (and import it into our main.rs). When you consider it, the reason is once again obvious: our declarative macro is expanded in our main function.