json 反序列化 reqwest 响应时未找到方法

json method not found when Deserializing a reqwest response

我是 Rust 的新手,我似乎找不到解决这个问题的方法。我正在尝试将 get 请求的响应作为 json。

#[macro_use]
extern crate serde;
extern crate serde_derive;
extern crate reqwest;
use reqwest::Error;

fn main(){
    #[derive(Deserialize)]

struct Ip {
    origin: String,
}

let json: Ip = reqwest::get("http://httpbin.org/ip").json();
//reqwest::get("http://httpbin.org/ip")?.json()?;
}

这里是cargo.toml

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_derive = "1.0"
reqwest = { version = "0.10", features = ["blocking"] }

我不断收到的错误是

此外, 如果我使用

reqwest::get("http://httpbin.org/ip")?.json()?;

(加上问号) 我收到另一个错误提示

cannot use the `?` operator in a function that returns `()`
this function should return `Result` or `Option` to accept `?`

我该如何解决这些问题?

根据 doc,您需要在 Cargo.toml:

中启用 json reqwest 功能
reqwest = { version = "0.10", features = ["blocking", "json"] }

此外,reqwest::get is part of the async API. Since your main is synchronous, you want reqwest::blocking::get