在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!
宏和过程宏一起使用。因此,我很困惑。
答案 0 :(得分:5)
use
应该与macro_rules!
宏和程序宏一起使用
除了assert
是neither 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.
#[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中较低的位置
Stabilize uniform paths on Rust 2018 (#56417)确实提到了这些:
内置宏,例如
use env
。 当前由于内置宏的某些(固定)实现细节而导致错误。 在稳定之前(消除错误之后),没有已知的问题需要解决。