Search code examples
macrosrustglium

How to import macros in Rust?


I'm struggling with how to import macros from an external crate. In my main.rs I'm importing the Glium crate:

#![macro_use]
extern crate glium;

pub use glium::*;

// where my actual main function will be done from
mod part01drawtriangle;

fn main() {
    part01drawtriangle::main();
}

In my other file, where my main function is coming from, I call one of the macros from that crate:

pub fn main() {
    implement_vertex!(Vertex, position);
}

When building, I get the error message:

error: macro undefined: 'implement_vertex!'

Solution

  • #[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).