2021年1月3日星期日

Why get method is not returning a Response object in reqwest?

I'm trying to replicate an example from reqwest documentation. Here is the example:

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

After I have added the reqwest = "0.10.10" line in the file Cargo.toml, I add the following code in my main.rs file:

extern crate reqwest;    fn main() {      let body = reqwest::get("https://www.rust-lang.org")?.text()?;      println!("body = {:?}", body);  }  

This code don't compile and returns the following error:

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

I'm a bit surprised with this behaviour since my code is almost ipsis litteris the documentation code.

I think that ? works only on Response objects, so I checked which object the get method is returning:

extern crate reqwest;    fn print_type_of<T>(_: &T) {      println!("{}", std::any::type_name::<T>())  }    fn main() {      let body = reqwest::get("https://www.rust-lang.org");      print_type_of(&body);  }  

output:

core::future::from_generator::GenFuture<reqwest::get<&str>::  

I mean, why I'm not getting an Response object instead, just like the documentation?

https://stackoverflow.com/questions/65556721/why-get-method-is-not-returning-a-response-object-in-reqwest January 04, 2021 at 09:38AM

没有评论:

发表评论