【发布时间】:2022-02-16 07:57:02
【问题描述】:
我一直在我的一个程序中使用#[tokio::main] 宏。导入main后不合格使用后,遇到意外错误。
use tokio::main;
#[main]
async fn main() {}
error[E0659]: `main` is ambiguous
--> src/main.rs:3:3
|
3 | #[main]
| ^^^^ ambiguous name
|
= note: ambiguous because of a name conflict with a builtin attribute
= note: `main` could refer to a built-in attribute
我一直在搜索文档,但在任何地方都找不到这个内置的 #[main] 属性。 The Rust Reference contains an index of built-in attributes。该索引不包括#[main],但确实包括an attribute named #[no_main]。
我搜索了rustlang/rust 存储库,找到了some code that seems related,但它似乎使用了一对名为#[start] 和#[rustc_main] 的宏,而没有提及#[main] 本身。 (这两个宏似乎都不能在 stable 上使用,#[start] 发出一个不稳定的错误,#[rustc_main] 发出一个错误,它仅供编译器内部使用。)
从名称中我猜想它是为了将不同的函数标记为入口点而不是 main,但它似乎也没有这样做:
#[main]
fn something_else() {
println!("this does not run");
}
fn main() {
println!("this runs");
}
Rust Analyzer 提供的不多:
除了与我的导入冲突之外,内置的 #[main] 属性还有什么作用? ????
【问题讨论】:
标签: rust main entry-point rust-macros