Created
May 1, 2023 18:46
-
-
Save mykhailokrainik/4049ce0e72f0c6bb6d79656476643a6e to your computer and use it in GitHub Desktop.
Example a future that resolves to return function value using Pin<Box<dyn Future<Output = u32>>>.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::pin::Pin; | |
use std::future::Future; | |
async fn async_fn() -> u32 { | |
42 | |
} | |
fn main() { | |
let future = Box::pin(async_fn()); | |
let pinned_future: Pin<Box<dyn Future<Output = u32>>> = future; | |
// You can now await the future without worrying about it moving in memory | |
let result = futures::executor::block_on(pinned_future); | |
println!("Result: {}", result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment