Async

The term async is a keyword in the Rust programming language. It is used to tell the compiler that a function is able to be processed asynchronously. This keyword can be seen as syntactic sugar. Every function preceded with the async keyword will be translated into a function that returns a Future containing the processing logic of the original function.

Let's picture it with a very lightweight example. Assume we will have a function that should return a number, but should be able to be processed asynchronously. With the keyword mentioned we could write this code:

async fn give_number() -> u32 {
    100
}
fn main() {}

With the async hint the compiler will de-sugar the function into one that returns a Future like demonstrated by the following code.

fn give_number() -> impl Future<Output = u32> {
    GiveNumberFuture
}

struct GiveNumberFuture {}

impl Future for GiveNumberFuture {
    // the return type of the original async function
    type Output = u32;
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        // the expression returning the value of the async function wrapped
        // into the Poll::Ready state
        Poll::Ready(100)
    }
}

Note The given example of desuggering the async function is used for demonstration purposes only and might not match the actual code generated by the compiler.