簡體   English   中英

編譯 rust 項目時,function 聲明中缺少 `async` 關鍵字

[英]the `async` keyword is missing from the function declaration when compile rust project

當我使用這個命令 cargo build 編譯 rust 項目時,顯示如下錯誤:

➜  reddwarf-admin git:(main) ✗ cargo build
   Compiling reddwarf-admin v0.1.0 (/Users/xiaoqiangjiang/source/reddwarf/backend/reddwarf-admin)
error: the `async` keyword is missing from the function declaration
  --> src/main.rs:48:7
   |
48 | async fn main() {
   |       ^^

這是我的 fn function 看起來像:

#[rocket::main]
#[tokio::main]
async fn main() {
    tokio::spawn(refresh_channel_rep());
    tokio::spawn(refresh_channel_article_count());
    tokio::spawn(remove_low_quality_articles());
    tokio::spawn(calculate_article_trend());

    let launch_result = create_server().launch().await;
    match launch_result {
        Ok(_) => println!("Rocket shut down gracefully."),
        Err(err) => println!("Rocket had an error: {}", err),
    };
}

我已經嘗試在main.rs中添加 tokio micro,如下所示:

#[macro_use]
extern crate tokio;

問題還是沒有解決,為什么會這樣? 我應該怎么做才能解決這個問題?

你不應該同時使用#[rocket::main]#[tokio::main] ,因為第一個比第二個更嚴格 - tokio::main 只是啟動運行時rocket::main也一樣根據 Rocket 的配置進行配置。

只需使用#[rocket::main] ,一切都會正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM