module - 如何跨模块文件使用宏?

标签 module rust rust-macros

我在同一个 crate 中的不同文件中有两个模块,其中 crate 启用了 macro_rules。我想在另一个模块中使用一个模块中定义的宏。

// macros.rs
#[macro_export] // or not? is ineffectual for this, afaik
macro_rules! my_macro(...)

// something.rs
use macros;
// use macros::my_macro; <-- unresolved import (for obvious reasons)
my_macro!() // <-- how?

我目前遇到编译器错误“macro undefined: 'my_macro'”...这是有道理的;宏系统在模块系统之前运行。我该如何解决这个问题?

最佳答案

同一个 crate 中的宏

新方法(自 Rust 1.32,2019-01-17 起)

foo::bar!();  // works

mod foo {
    macro_rules! bar {
        () => ()
    }

    pub(crate) use bar;    // <-- the trick
}

foo::bar!();  // works

通过pub use,可以像其他任何项目一样使用和导入宏。并且与旧方法不同,这不依赖于源代码顺序,因此您可以在定义(源代码顺序)之前使用宏。

旧方法

bar!();   // Does not work! Relies on source code order!

#[macro_use]
mod foo {
    macro_rules! bar {
        () => ()
    }
}

bar!();    // works

如果你想在同一个 crate 中使用宏,定义你的宏的模块需要属性 #[macro_use]。请注意,宏只能定义后使用!



跨 crate 的宏

Crate util

#[macro_export]
macro_rules! foo {
    () => ()
}

创建用户

use util::foo;

foo!();

请注意,使用此方法,宏始终位于 crate 的顶层!因此,即使 foo 位于 mod bar {} 内,user crate 仍然必须编写 use util::foo ; 并且 使用 util::bar::foo;。通过使用 pub use,您可以从 crate 的模块中导出宏(除了在根目录中导出宏之外)。

在 Rust 2018 之前,您必须通过将属性 #[macro_use] 添加到 extern crate util; 语句来从其他 crate 导入宏。这将从 util 导入所有宏。不再需要此语法。

关于module - 如何跨模块文件使用宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26731243/

相关文章:

java - 有没有一种简化的方法来从 Rust 调用 Java 函数?

rust - 实现 proc 宏时的循环包依赖

ruby-on-rails - rails : Cannot include PgSearch Module provided by the pg_search Gem

jboss - 如何使用模块将库添加到 Wildfly Application Server?

multithreading - 我如何总结使用 Rust 从 1 到 1000000 的并发性?

rust - Treasury Module 的 set_pot 函数是否可供公众使用?

rust - 如何从 Rust 调用内置的 Dyon 函数?

Python 和 py2exe - 隐式导入模块

Linux 内核 alloc 缓存去除 const 警告

rust - 如何区分macro_rules宏中的不同种类的项目?