1

I'm a newbie in rust lang. What does returning -> _ mean for the rocket function?

#[launch]
fn rocket() -> _ {
    rocket::build()
        .attach(json::stage())
        .attach(msgpack::stage())
        .attach(uuid::stage())
}
Jmb
  • 18,893
  • 2
  • 28
  • 55

2 Answers2

6

The #[launch] macro is trying to be clever here. The documentation reads:

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

So its just a shorthand provided by the macro to avoid specifying that return type.

Normally when a _ is used in place of a type, it is a placeholder for the compiler to infer it based on context. However, this is not allowed for return types since Rust requires function signatures to be explicit. See the error here on the playground. Attribute macros usually require the item they're decorated on to be valid Rust, but using _ is still syntactically valid (even though its not semantically valid) so it is allowed.

kmdreko
  • 42,554
  • 6
  • 57
  • 106
3

This is not usually valid Rust. The #[launch] attribute runs a macro which transforms the function that it annotates, adding support for this syntax.

The syntax means that the return type of the function should be inferred from the function's body.

See:

Peter Hall
  • 53,120
  • 14
  • 139
  • 204