logo

Attribute Macro rocket::launch

source · []
#[launch]
Expand description

Generates a main function that launches a returned Rocket<Build>.

When applied to a function that returns a Rocket<Build> instance, #[launch] automatically initializes an async runtime and launches the function’s returned instance:

use rocket::{Rocket, Build};

#[launch]
fn rocket() -> Rocket<Build> {
    rocket::build()
}

This generates code equivalent to the following:

#[rocket::main]
async fn main() {
    // Recall that an uninspected `Error` will cause a pretty-printed panic,
    // so rest assured failures do not go undetected when using `#[launch]`.
    let _ = rocket().launch().await;
}

To avoid needing to import any items in the common case, the launch attribute will infer a return type written as _ as Rocket<Build>:

#[launch]
fn rocket() -> _ {
    rocket::build()
}

The attributed function may be async:

#[launch]
async fn rocket() -> _ {
    some_async_work().await;
    rocket::build()
}