Is this an error on Reqwest blocking.send documentation?

Hi! I'm very new to Rust, apologies if this looks lame. I found this example in Reqwest::blocking documentation Client in reqwest::blocking - Rust :

use reqwest::blocking::Client;
let client = Client::new();
let resp = client.get("http://httpbin.org/").send()?;

But when I copy almost identical I get the following compilation error:

error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
  --> src/main.rs:10:13
   |
5  | / fn main() {
6  | |     let client = Client::new();
7  | |
8  | |     let response = client.get(
9  | |         "https://httpbin.org"
10 | |     ).send()?;
   | |             ^ cannot use the `?` operator in a function that returns `()`
11 | |     println!("{:?}",response);
12 | | }
   | |_- this function should return `Result` or `Option` to accept `?`
   |
   = help: the trait `FromResidual<Result<Infallible, reqwest::Error>>` is not implemented for `()`

I've checked and it is reqwest v0.11.12 for both the documentation and my installed version.

Is this an error in the docs? AFAIK the examples are used as tests when compiling the crate so this shouldn't pass... I would be happy to raise de issue on github but I feel that I'm missing something here...

My full code:

use reqwest::blocking::Client;
use std::collections::HashMap;


fn main() {
    let client = Client::new();

    let response = client.get(
        "https://httpbin.org"
    ).send()?;
    println!("{:?}",response);
}

Cargo.toml

[package]
name = "harvester_siem"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

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

The try operator ? can only be used on Result values in functions that return Result. You can replace it with unwrap to make the program exit if the Result represents and error, or expect to supply an additional message to print when the program exits.

1 Like

Thanks @semicoleon! Now I get it! The missing part was the return value for my main function. For the sake of completeness, this is how it worked:

use reqwest::blocking::Client;
use std::collections::HashMap;

fn main() -> Result<(), reqwest::Error> {
    let client = Client::new();

    let response = client.get(
        "https://httpbin.org"
    ).send()?;
    println!("{:?}",response);
    Ok(())
}
2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.