How to specify an output type for a Future/async block?

use tokio::runtime::Runtime;

// Create the runtime
let rt  = Runtime::new().unwrap();

// Execute the future, blocking the current thread until completion
let s = rt.block_on(async {
    println!("hello");
});

is it possible to specify an output type for a future block? On the code above, s: (), but I wanted to be Result<(), Error> so I can return some error from inside the block.

You can place Ok(()) at the end of the closure to tell the compiler you want to return a Result. If the compiler still can't infer the error type, you can further write Ok::<_, Error>(()) to specify that it returns a Result<(), Error>.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.