Reputation: 16660
I have started to recently use futures in Rust, but I couldn't find any way to print the future value for debugging purpose. I get this error even with the formatting helper:
^^^^^^^^ `futures::Future<Item=hyper::Response, Error=hyper::Error>` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
for the below code
#[cfg(test)]
println!(">>>>>>>> Future value returned {:?}", future);
Is there any existing solution (macros) to debug this?
Upvotes: 7
Views: 3206
Reputation: 676
You have to either specify the concrete type (not trait) that implements Debug
or you have to rely on the impl Trait
declaration but ensure that you also implement 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());
}
The trait itself does not require the underlying struct to implement Debug
. Even if the struct implements Debug, you have to make sure when returning the trait instead of the struct that you declare it to implement Debug
. It should compile then.
Upvotes: 1