我是rust的新手,我正在尝试获取请求响应主体,我当前的代码是:
let client = reqwest::Client::new();
let response_text = client.get("google").send();
println!("{}", response_text);但这会产生错误
std::result::Result<reqwest::Response, reqwest::Error>` cannot be formatted with the default formatter发布于 2020-09-11 08:04:24
简短版本
使用"{:?}"而不是"{}"调试输出。
长版本
我不得不修改您的代码以获得相同的错误消息:
pub fn dummy() {
let client = reqwest::blocking::Client::new();
let response_text = client.get("google").send();
println!("{}", response_text);
}它使用阻塞API而不是异步API。异步API给出了一个类似的但稍微复杂的错误。
完整的错误消息是:
error[E0277]: `std::result::Result<reqwest::blocking::Response, reqwest::Error>` doesn't implement `std::fmt::Display`
--> src/lib.rs:5:20
|
5 | println!("{}", response_text);
| ^^^^^^^^^^^^^ `std::result::Result<reqwest::blocking::Response, reqwest::Error>` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `std::result::Result<reqwest::blocking::Response, reqwest::Error>`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: required by `std::fmt::Display::fmt`
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)第一行给出了std::result::Result<reqwest::blocking::Response, reqwest::Error>没有实现std::fmt::Display的根本原因。
问题是使用"{}“进行格式化需要一个支持std::fmt::Display的对象,而Result对象不支持。(您可以通过查看它在https://doc.rust-lang.org/std/result/enum.Result.html中实现的特征列表来检查这一点)
Result确实实现了std::fmt::Debug,只要它的两个参数都实现了,就像这里的情况一样。这是使用"{:?}"而不是{}进行格式化所必需的特性。
如果你不想要调试输出,你需要更努力的工作来获得内容。你会想要这样的东西
match response_text {
Err(e) => eprintln!("Error: {:?}", e);
Ok(v) => println!("Body: {}", v.text().unwrap();
}但是,即使这样也不能正确处理非UTF8输入的情况,并且展开将会死机。
https://stackoverflow.com/questions/63838828
复制相似问题