1

I'm trying to get a simple API up and running using rocket similar to this repo:

//! main.rs
use ptb_api; // my crate

#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
    ptb_api::rocket().launch().await
}
//! lib.rs
use rocket::{get, launch, routes};

#[get("/")]
fn index() -> &'static str {
    "Hello, world!"
}

#[launch]
pub fn rocket() -> _ {
    rocket::build().mount("/", routes![index])
}
# Cargo.toml
[dependencies]
rocket = "0.5.0-rc.2"

When I attempt to build I get this error:

error[E0308]: mismatched types
 --> src\main.rs:4:46
  |
4 |   async fn main() -> Result<(), rocket::Error> {
  |  ____________________-------------------------_^
  | |                    |
  | |                    expected `Result<(), rocket::Error>` because of return type
5 | |     ptb_api::rocket().launch().await
6 | | }
  | |_^ expected `()`, found struct `Rocket`
  |
  = note: expected enum `Result<(), _>`
             found enum `Result<Rocket<Ignite>, _>`

I am using nearly the same code as the repo I linked above. I can't tell why the linked repo builds and mine fails. It should be worth noting that I am using rocket version 0.5.0-rc.2 and the linked repo was using 0.5.0-rc.1. And after cloning the repo and upgrading it, the same problem occurs. So something changed in rocket between release candidates, just not sure what.

kmdreko
  • 42,554
  • 6
  • 57
  • 106
stegnerd
  • 1,119
  • 1
  • 17
  • 25

1 Answers1

2

The 0.5.0-rc.1 docs show launch's signature as:

pub async fn launch(self) -> Result<(), Error>

But the 0.5.0-rc.2 docs show launch's signature as:

pub async fn launch(self) -> Result<Rocket<Ignite>, Error>

Thus, the return type using #[launch] no longer matches your signature in main() after upgrading. This can be fixed by simply returning Ok at the end of your function as shown here:

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

    Ok(())
}
kmdreko
  • 42,554
  • 6
  • 57
  • 106
jh316
  • 901
  • 7
  • 9
  • nice catch about the return type. I have tried what you suggested about updating main and I get this error: ```error[E0277]: `main` has invalid return type `Rocket` --> src/main.rs:50:19 | 50 | async fn main() ->Result, Error> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `main` can only return types that implement `Termination` | = help: the trait `Termination` is not implemented for `Rocket` = note: required because of the requirements on the impl of `Termination` for `Result, rocket::Error>``` if I do Ok(()) works – stegnerd Aug 01 '22 at 21:25
  • by that I mean my original signature and Ok(()) so it really is returning a () like these docs suggest https://rocket.rs/v0.5-rc/guide/overview/#launching – stegnerd Aug 01 '22 at 21:28
  • @stegnerd Good catch, I'll update my answer with what you found. – jh316 Aug 01 '22 at 21:37