我最近开始在 Rust 中使用 futures,但我找不到任何方法来打印 future 值以进行调试。即使使用格式化助手,我也会收到此错误:
^^^^^^^^ `futures::Future<Item=hyper::Response, Error=hyper::Error>` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
Run Code Online (Sandbox Code Playgroud)
对于下面的代码
#[cfg(test)]
println!(">>>>>>>> Future value returned {:?}", future);
Run Code Online (Sandbox Code Playgroud)
是否有任何现有的解决方案(宏)来调试这个?
您必须指定实现的具体类型(而不是特征),Debug或者必须依赖impl Trait声明但确保您也实现Debug.
extern crate futures;
use futures::{Future, future::FutureResult};
use std::fmt::Debug;
fn get_default_future<'s>() -> FutureResult<&'s str, ()> {
futures::future::ok::<_, ()>("foo")
}
fn get_printable_future() -> impl Future + Debug {
futures::future::ok::<_, ()>("bar")
}
fn main() {
println!("{:?}", get_default_future());
println!("{:?}", get_printable_future());
}
Run Code Online (Sandbox Code Playgroud)
该特征本身不需要底层结构来实现Debug。即使该结构实现了 Debug,您也必须确保在返回特征而不是您声明它要实现的结构时Debug。然后它应该编译。
| 归档时间: |
|
| 查看次数: |
2988 次 |
| 最近记录: |