How to import all macros, derives, and procedural macros in Rust 2018 without using extern crate?

Deadly 提交于 2019-12-10 03:51:31

问题


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.


回答1:


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.




回答2:


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::*;


来源:https://stackoverflow.com/questions/50999749/how-to-import-all-macros-derives-and-procedural-macros-in-rust-2018-without-us

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!