将带有:的参数传递给宏调用时发生编译错误



我正在尝试一个类似以下的宏

from_converter!(std::io::Error, MyError);
macro_rules! from_converter {
($e: tt, $n: tt) => {
impl std::convert::From<$e> for Error {
fn from(source: $e) -> Self {
$n.into_error(source)
}
}
};
}

我得到的编译错误是:

no rules expected the token `::`
no rules expected this token in macro callrustc
error.rs(37, 1): when calling this macro
error.rs(47, 20): no rules expected this token in macro call

tt是一个TokenTree,意思是一个奇异的令牌。第一个($e(应该是类型,表示为ty,第二个($n(应该是表达式,表示为expr:

macro_rules! from_converter {
($e: ty, $n: expr) => { /* ... */ };
}

最新更新