【问题标题】:How to import all macros, derives, and procedural macros in Rust 2018 without using extern crate?如何在不使用 extern crate 的情况下导入 Rust 2018 中的所有宏、派生和过程宏?
【发布时间】:2018-10-29 22:22:12
【问题描述】:

我正在试验Rust Edition 2018。在 Rust 2015 中,您使用

#[macro_use]
extern crate log;

用于导入宏。在 Rust 2018 中,extern crate 可能是单调的。有没有办法在没有extern crate 的情况下从包中导入所有宏?对于简单的宏,importing it in the modules 可以,但复杂的宏依赖于其他几个宏,不方便。

【问题讨论】:

标签: rust rust-2018


【解决方案1】:

我没有看到任何导入所有宏的方法,但如果您可以导入 crate 提供的所有基本对象,您通常应该通过以下方式获取所有宏:

use the_crate_with_macros::*;

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

这也适用于从 1.30 版开始的 Rust 2015。

【讨论】:

    【解决方案2】:

    正如您已经说过的,您可以通过

    导入单个宏
    use foo::mac1;
    

    要一次导入多个宏,您可以使用nested imports

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

    或者依赖 crate 作者,他们会让你通过单个 glob 导入它,例如

    use foo::macros::*;
    

    【讨论】:

      猜你喜欢
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多