为什么我不能通过`use`导入`std :: assert`,而它适用于std的其他宏?

问题描述 投票:4回答:1

使用Rust 2018,此代码有效(Playground):

use std::panic;
use std::format;
use std::assert_eq;

但是这个:

use std::assert;

结果出现此错误:

error[E0432]: unresolved import `std::assert`
 --> src/lib.rs:4:5
  |
4 | use std::assert;
  |     ^^^^^^^^^^^ no `assert` in the root

我读了the edition guide about this topic,它说use应该与macro_rules!宏和程序宏一起使用。因此,我很困惑。

rust macros
1个回答
5
投票

use应该使用macro_rules!宏和程序宏

除了assertneither of those

/// Built-in macros to the compiler itself.
///
/// These macros do not have any corresponding definition with a `macro_rules!`
/// macro, but are documented here. Their implementations can be found hardcoded
/// into libsyntax itself.

这是一个compiler built-in

#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_doc_only_macro]
macro_rules! assert {
    ($cond:expr) => ({ /* compiler built-in */ });
    ($cond:expr,) => ({ /* compiler built-in */ });
    ($cond:expr, $($arg:tt)+) => ({ /* compiler built-in */ });
}

其他虚假宏包括:

  • compile_error
  • format_args
  • env
  • option_env
  • concat_idents
  • concat
  • line
  • column
  • file
  • stringify
  • include_str
  • include_bytes
  • module_path
  • cfg
  • include

assertlibsyntax_ext/assert.rs的实际定义要低得多


Stabilize uniform paths on Rust 2018 (#56417)确实提到了这些:

内置宏,例如use env。目前由于内置宏的一些(可修复的)实现细节而导致错误。在稳定之前没有已知的问题需要解决(在删除错误之后)。

© www.soinside.com 2019 - 2024. All rights reserved.