Attribute Macro rocket::main[][src]

#[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> {
    rocket::build()
        .ignite().await?
        .launch().await
}

It should be used only when inspection of an ignited instance of Rocket is required, or when the return value of launch() is to be inspected:

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

    let result = rocket.launch().await;
    println!("The server shutdown: {:?}", result);

    result
}

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.