Macro Syntax ($crate)


#1

In iron 0.6.0 there is defined a macro that uses $crate in the expansion. I cannot find here that is defined. I assume it means the crate this macro is defined in but am unsure

The most useful resource I have currently for understanding macros is: https://danielkeep.github.io/tlborm/book/mbe-macro-rules.html. I clearly need a better one…

macro_rules! itry {
    ($result:expr) => (itry!($result, $crate::status::InternalServerError));

    ($result:expr, $modifier:expr) => (match $result {
        ::std::result::Result::Ok(val) => val,
        ::std::result::Result::Err(err) => return ::std::result::Result::Err(
            $crate::IronError::new(err, $modifier))
    })
}


#2

The only reference I’ve known is from the first edition of the book. It’s pretty much a special variable.

Within a macro imported from a crate named foo , the special macro variable $crate will expand to ::foo . By contrast, when a macro is defined and then used in the same crate, $crate will expand to nothing.


#3

The $crate was introduced so you can write something like $crate::utils::my_func() in a macro and know that it’s calling the correct thing.

I’m assuming this was just an oversight while writing the book, especially considering macros are only an appendix in the current version. @steveklabnik may be able to tell you more though, seeing as he’s the lead for Rust’s docs team.