0

I was attempting a macro like following

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)
            }
        }
    };
}

The compilation error I get is:

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
user855
  • 19,048
  • 38
  • 98
  • 162

1 Answers1

3

tt is a TokenTree, which means a singular token. The first one ($e) should be a type, denoted as ty, and the second ($n) should be an expression, denoted as expr:

macro_rules! from_converter {
    ($e: ty, $n: expr) => { /* ... */ };
}
Aplet123
  • 33,825
  • 1
  • 29
  • 55