Skip to content

Instantly share code, notes, and snippets.

@misha-krainik
Created May 1, 2023 18:46
Show Gist options
  • Save misha-krainik/4049ce0e72f0c6bb6d79656476643a6e to your computer and use it in GitHub Desktop.
Save misha-krainik/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>>>.
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