使用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
应该使用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.
这是一个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
在assert
,libsyntax_ext/assert.rs的实际定义要低得多
Stabilize uniform paths on Rust 2018 (#56417)确实提到了这些:
内置宏,例如
use env
。目前由于内置宏的一些(可修复的)实现细节而导致错误。在稳定之前没有已知的问题需要解决(在删除错误之后)。