How to import all macros, derives, and procedural

2019-04-23 18:22发布

I'm experimenting with Rust Edition 2018. In Rust 2015 you use

#[macro_use]
extern crate log;

for importing macros. In Rust 2018 extern crate is probably unidiomatic. Is there a way, to import all macros from the crate without extern crate? For simple macros, importing it in the modules is fine, but complicated macros depend on several other macros, which is unhandy.

2条回答
Emotional °昔
2楼-- · 2019-04-23 18:28

As you already stated you can import a single macro via

use foo::mac1;

To import multiple macros at once you can either use nested imports

use foo::{mac1, mac2, mac3};

or rely on the crate author that they will let you import it via a single glob, e.g.

use foo::macros::*;
查看更多
手持菜刀,她持情操
3楼-- · 2019-04-23 18:47

I don't see any way of importing only all the macros, but if you are fine with importing all the essential objects a crate provides, you should usually get all the macros by writing:

use the_crate_with_macros::*;

or

use the_crate_with_macros::prelude::*; // if available

This also works in Rust 2015 starting in version 1.30.

查看更多
登录 后发表回答