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



在 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

我阅读了有关此主题的版本指南,它说use应该使用macro_rules!宏和过程宏。因此,我很困惑。

use应使用macro_rules!宏和过程宏

除了assert都不是:

/// 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.

它是一个内置的编译器:

#[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

assert的实际定义在 libsyntax_ext/assert.rs 中埋藏得更低


Rust 2018 上的稳定统一路径 (#56417( 确实顺便提到了这些:

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

相关内容

最新更新