简体   繁体   English

编译 rust 项目时,function 声明中缺少 `async` 关键字

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

when I using this command cargo build to compile the rust project, shows error like this:当我使用这个命令 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() {
   |       ^^

this is my fn function look like:这是我的 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),
    };
}

I have already tried to add the tokio micro in the main.rs like this:我已经尝试在main.rs中添加 tokio micro,如下所示:

#[macro_use]
extern crate tokio;

the problem still did not solved, why did this happen?问题还是没有解决,为什么会这样? what should I do to fixed this problem?我应该怎么做才能解决这个问题?

You shouldn't use both #[rocket::main] and #[tokio::main] , since the first does strictly more then the second - tokio::main just starts the runtime , rocket::main does the same and also configures it according to the Rocket configuration.你不应该同时使用#[rocket::main]#[tokio::main] ,因为第一个比第二个更严格 - tokio::main 只是启动运行时rocket::main也一样根据 Rocket 的配置进行配置。

Just use the #[rocket::main] only, and everything should work.只需使用#[rocket::main] ,一切都会正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM