Search code examples
rustrust-tokiorust-rocket

Why is "the `async` keyword missing" when using Tokio and Rocket?


When compiling my Rust project, I get this error:

error: the `async` keyword is missing from the function declaration
  --> src/main.rs:48:7
   |
48 | async fn main() {
   |       ^^

My main() looks like:

#[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),
    };
}

Why did this happen? What should I do to fixed this problem?


Solution

  • 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.

    Just use the #[rocket::main] only, and everything should work.