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

标签 rust macros

对于 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! 宏和过程宏一起使用。因此,我很困惑。

最佳答案

use should work with macro_rules! macros and procedural macros

除了 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 */ });
}

其他伪宏包括:

  • 编译错误
  • format_args
  • 环境
  • option_env
  • concat_idents
  • 连接
  • 文件
  • 字符串化
  • include_str
  • include_bytes
  • 模块路径
  • 配置
  • 包含

assert 的实际定义隐藏在 libsyntax_ext/assert.rs


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

Built-in macros, for example use env. Currently an error due to some (fixable) implementation details of built-in macros. No known issues to resolve before stabilization (after the error is removed).

关于rust - 为什么我不能通过 `std::assert` 导入 `use` 而它适用于来自 std 的其他宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55444086/

相关文章:

ios - 为 GCD 调用编写宏?

variables - 如何在 TWIG 宏中访问模板变量?

objective-c - 复杂的预处理器宏

c++ - 为什么 UINTX_C() 宏没有在 Windows stdint.h 中正确定义?

分配最少的随机字符串生成器

reflection - Rust 是如何实现反射的?

string - 如何在Rust中填充左手? [复制]

multithreading - 如何启动和停止工作线程

rust - 我正在安装 “parquet-schema: command not found”,尽管我已经完成了 cargo 安装拼花地板

C block 变成表达式: ( {int a = 1; int b = 2; a+b;} ) equals 3