How to import macros in Rust?

According to the Rust Edition Guide:

In Rust 2018, you can import specific macros from external crates via use statements, rather than the old #[macro_use] attribute.

// in a `bar` crate's lib.rs:
#[macro_export]
macro_rules! baz {
    () => ()
}

// in your crate:
use bar::baz;

// Rather than:
//
// #[macro_use]
// extern crate bar;

This only works for macros defined in external crates. For macros defined locally, #[macro_use] mod foo; is still required, as it was in Rust 2015.


#[macro_use], not #![macro_use].

#[..] applies an attribute to the thing after it (in this case, the extern crate). #![..] applies an attribute to the containing thing (in this case, the root module).

Tags:

Macros

Rust

Glium