-1

I would like to call two functions that perform an HTTP call with reqwest on two different branches of a match:

async fn list_projects() -> Result<(), reqwest::Error> {
    let body = reqwest::get("https://...")
        .await?
        .text()
        .await?;
    println!("{:?}", body);
    Ok(())
}

async fn list_categories() -> Result<(), reqwest::Error> {
    let body = reqwest::get("http://...")
        .await?
        .text()
        .await?;
    println!("{:?}", body);
    Ok(())
}

match &args.list {
    List::projects => list_projects(),
    List::categories => list_categories()
}

I get the following error:

async fn list_categories() -> Result<(), reqwest::Error> {
   |                                 -------------------------- the `Output` of this `async fn`'s found opaque type
...
49 | /     match &args.list {
50 | |         List::projects => list_projects(),
   | |                           --------------- this is found to be of type `impl std::future::Future`
51 | |         List::categories => list_categories()
   | |                             ^^^^^^^^^^^^^^^^^ expected opaque type, found a different opaque type
52 | |     }
   | |_____- `match` arms have incompatible types
   |
   = note:     expected type `impl std::future::Future` (opaque type at <src/main.rs:29:29>)
           found opaque type `impl std::future::Future` (opaque type at <src/main.rs:38:31>)
   = note: distinct uses of `impl Trait` result in different opaque types
   = help: if both `Future`s have the same `Output` type, consider `.await`ing on both of them

Should I use std::future::Either?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
C Taque
  • 997
  • 2
  • 15
  • 31

1 Answers1

0

Every async function has a different type. If your real code isn't something more complex you should be able to do what the message suggests:

consider .awaiting on both of them

Jussi Kukkonen
  • 13,857
  • 1
  • 37
  • 54
  • I cannot set main as async, so I cannot await the function calls – C Taque May 26 '20 at 16:55
  • @cta `async-std` has an [async_main](https://docs.rs/async-std/1.6.0/async_std/attr.main.html) attribute macro you can use to turn your `main` into an `async fn`. – IInspectable May 26 '20 at 17:05