无法导入/导出宏

SBS*_*STP 4 macros rust

我有一个名为macros.rscontains 的模块

/// Macro to easily implement FromError.
/// from_error!(MyError, IoError, MyError::IoError)
macro_rules! from_error {
    ( $t:ty, $err:ty, $name:path ) => {
        use std;
        impl std::error::FromError<$err> for $t {
            fn from_error(err: $err) -> $t {
                $name(err)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的main.rs导入模块这样

#[macro_use] mod macros;
Run Code Online (Sandbox Code Playgroud)

当我尝试from_error在项目的其他模块中使用时,编译器说error: macro undefined: 'from_error!'.

SBS*_*STP 9

事实证明,您声明模块的顺序很重要.

mod json; // json uses macros from the "macros" module. can't find them.
#[macro_use]
mod macros;

#[macro_use]
mod macros;
mod json; // json uses macros from the "macros" module. everything's ok this way.
Run Code Online (Sandbox Code Playgroud)