0

I tried to complied the following reqwest example:

let client = reqwest::Client::new();
let res = client.post("http://httpbin.org/post")
    .body("the exact body that is sent")
    .send()?;

The example fails to compile:

error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
  --> src/main.rs:26:15
   |
26 |       let res = client.post("http://httpbin.org/post")
   |  _______________^
27 | |     .body("the exact body that is sent")
28 | |     .send()?;
   | |____________^ the `?` operator cannot be applied to type `impl std::future::Future`
   |
   = help: the trait `std::ops::Try` is not implemented for `impl std::future::Future`
   = note: required by `std::ops::Try::into_result`

error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
  --> src/main.rs:26:15
   |
11 |  / fn main(){
12 |  |     // gets the api key from env var
13 |  |     let mut key: String = env::var("key").unwrap();
14 |  |     // get the mok key bassically useless but its still useful to prevent tampering
...   |
26 |  |     let res = client.post("http://httpbin.org/post")
   |  |_______________^
27 | ||     .body("the exact body that is sent")
28 | ||     .send()?;
   | ||____________^ cannot use the `?` operator in a function that returns `()`
...   |
49 |  |     //}
50 |  | }
   |  |_- this function should return `Result` or `Option` to accept `?`
   |
   = help: the trait `std::ops::Try` is not implemented for `()`
   = note: required by `std::ops::Try::from_error`

How should I fix this? Is this out of date? I am using Rust version 1.47.0

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
dev
  • 95
  • 2
  • 12
  • If you just want to compile it, you can replace the `?` the compiler is complaining about with `.unwrap()`. – Sven Marnach Oct 13 '20 at 11:37
  • And no, the example isn't out of date. It just requires that the code is put inside a function returning a `Result`, which is a common requirement for example code. – Sven Marnach Oct 13 '20 at 11:38
  • adding unwrap throws this error `no method named 'unwrap' found for opaque type 'impl std::future::Future' in the current scope` – dev Oct 13 '20 at 11:41
  • This code may indeed be for a different version of `reqwest` than you are using. Please go to the same version of the documentation on `https://docs.rs/reqwest` as the version you are using. – Sven Marnach Oct 13 '20 at 11:43
  • 1
    Using `reqwest::blocking::Client` instead of `reqwest::Client` may do the trick. – Sven Marnach Oct 13 '20 at 11:43
  • could not find 'blocking' in 'reqwest' if used with .unwrap() – dev Oct 13 '20 at 11:45
  • i found the fix to the blocking import issue https://stackoverflow.com/questions/58906965/could-not-find-blocking-in-reqwest – dev Oct 13 '20 at 11:50
  • using blocking it seems to work – dev Oct 13 '20 at 11:54

1 Answers1

4

Based on the error message, you are using an async version of reqwest. The latest version contains both async and blocking versions, if I recall correctly.

In order to actually consume the future and get to the value inside, you need to execute it using an executor e.g. Tokio. This can be done in multiple ways.

The simplest way is to add tokio = { version = "0.2.22", features = ["macros"] } to your Cargo.toml and then have this in main.rs:

#[tokio::main]
async fn main() {
    let client = reqwest::Client::new();
    let res = client.post("http://httpbin.org/post")
        .body("the exact body that is sent")
        .send().await;
} 

Note that I removed the ? since the future does not resolve to a Result or Option.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
AdaShoelace
  • 342
  • 1
  • 14