Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print future value for debugging?

Tags:

rust

future

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?

like image 448
Aditya Singh Avatar asked Aug 16 '26 11:08

Aditya Singh


1 Answers

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.

like image 96
fyaa Avatar answered Aug 18 '26 05:08

fyaa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!