Attribute Macro rocket::main

source ·
#[main]
Expand description

Retrofits async fn support in main functions.

A main async fn function decorated with #[rocket::main] is transformed into a regular main function that internally initializes a Rocket-specific tokio runtime and runs the attributed async fn inside of it:

#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
    let _rocket = rocket::build()
        .ignite().await?
        .launch().await?;

    Ok(())
}

It should be used only when the return values of ignite() or launch() are to be inspected:

#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
    let rocket = rocket::build().ignite().await?;
    println!("Hello, Rocket: {:?}", rocket);

    let rocket = rocket.launch().await?;
    println!("Welcome back, Rocket: {:?}", rocket);

    Ok(())
}

For all other cases, use #[launch] instead.

The function attributed with #[rocket::main] must be async and must be called main. Violation of either results in a compile-time error.