Rust, asynchronized: what is a rust Future?

What is a Rust Future?

A Future in Rust is a trait that makes use of the .await or poll primitives.

#[must_use = "futures do nothing unless you `.await` or poll them"]
pub trait Future {
    type Output;
    pub fn poll(
        self: Pin<&mut Self>, 
        cx: &mut Context<'_>
    ) -> Poll<Self::Output>;
}

How do I define a future?

The easiest way to get started with Rust Futures is to declare a function as async. Once a function is declared async, the .await syntax can be used inside of the function to declare that the function must wait for the previous future to end before itself may continue. In this fashion asynchronous code comes to be synchronized at runtime. The thread allocation etc. is left to the runtime to determine what is best to run when and where.

use futures::executor::block_on;

async fn async_sum(v: Vec<u64>) -> u64 {
   v.iter().sum()
}

fn main() {
   let future1 = async {
      async_sum(vec![1,2,3,4,5]).await
   };
   println!("sum = {}", block_on(future1));
}