无法 import/export 宏
Unable to import/export macro
我有一个名为 macros.rs
的模块,其中包含
/// 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)
}
}
}
}
在我的 main.rs
中,我像这样导入模块
#[macro_use] mod macros;
当我尝试在项目的其他模块中使用 from_error
时,编译器显示 error: macro undefined: 'from_error!'
。
原来你声明模块的顺序很重要。
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.
我有一个名为 macros.rs
的模块,其中包含
/// 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)
}
}
}
}
在我的 main.rs
中,我像这样导入模块
#[macro_use] mod macros;
当我尝试在项目的其他模块中使用 from_error
时,编译器显示 error: macro undefined: 'from_error!'
。
原来你声明模块的顺序很重要。
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.