gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:53:25 26 4
gpt4 key购买 nike

对于 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com