0

I am trying to use the reqwest::blocking example, and I am encountering an error.

Firstly,

"the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)

cannot use the `?` operator in a function that returns `()`

help: the trait `std::ops::Try` is not implemented for `()`
note: required by `std::ops::Try::from_error`

when using the following code:

use reqwest;

fn main() {
    let body = reqwest::blocking::get("https://www.rust-lang.org")?
    .text()?;

    println!("body = {:?}", body);

}

After removing the ?s from the code:

no method named `text` found for enum `std::result::Result<reqwest::blocking::Response, reqwest::Error>` in the current scope

method not found in `std::result::Result<reqwest::blocking::Response, reqwest::Error>`
use reqwest;

fn main() {
    let body = reqwest::blocking::get("https://www.rust-lang.org")
    .text();

    println!("body = {:?}", body);

}

I have enabled the blocking feature in my cargo.toml:

[dependencies]
reqwest = {version = "0.11.3", features = ["blocking"]}

I was able to find an issue that is very similar to my problem, however it is now closed and locked. If there are any relevant details that I failed to mention, please tell me.

  • @loganfsmyth Thank you, that explains why the `?` does not work, but my main focus is on why the second revision of the code does not work. – ilikeapples1234 Jun 21 '21 at 01:00
  • 3
    You removed the `?` so the type of `body` is now a `Result`, as the error message says. The fix is to change the return type of `main` and put the `?` back. Otherwise you'd need to use `.unwrap` or `.expect`. – loganfsmyth Jun 21 '21 at 01:05
  • You cannot call `.text()` on a `Result` type because the inner type needs to be extracted with a method/member function call. – CinchBlue Jun 21 '21 at 01:16
  • @loganfsmyth I see. Thank you. – ilikeapples1234 Jun 21 '21 at 01:23

0 Answers0