0

Rust – async await example

This post provides an example of async , async fn , and await for asynchronous processing.

Requirements

The following are used for this post

  • Windows 10
  • Rust 1.39.0
  • IntelliJ IDEA
  • Rust Plugin for IntelliJ
  • futures crate
    • This is to allow us to execute asynchronous functions before the main function terminates
    • alternatively, async-std can be used for this post

Update Cargo.toml

Modify Cargo.toml as follows

The Codes

We put in some use  statements.

Then, we crate two asynchronous functions.

The second asynchronous function calls retrieval_val  and uses the .await  keyword to resolve an expected value.

Then, we have our main function. It invokes the pre_retrieval  function.

We use block_on  to force pre_retrieval  complete its execution before the main function terminates. When we run the codes, the output is something smiliar to

Karl San Gabriel

Karl San Gabriel

Professional Software Developer