Sitemap

Async Programming <Rust>

3 min readOct 22, 2026

--

A programming model for Multi-tasking in Rust, .await( ) and async function.

Asynchronous programming is a kind or category of concurrent programming where a task or a thread is allowed to run whilst another waits or sleep for a result or IO. Unlike in normal concurrency a thread or a task must finish before another begins meaning their is a block that prevents another task from executing until the first task finishes.

Async Rust is such that if the program can be executed immediately then output the result if not it waits to make space for another to run meaning it has a Future trait and can be polled(ready )or pending. Meaning there is some sort of scheduler or coordinate that carries out the job of arranging the task or threads for execution. Async and .await( ) are built in tools or methods for writing asynchronous functions , which implements a trait called future. As a result every async function in Rust is automatically part of the Future trait in Rust have a Future trait as well.

Any place where async code employs await means control is handed back to the runtime which is able to kick-off or start-off another task and comeback to the previous one when is ready in the future .

For example in the code below the fetch_user_data function can sleep or wait for the data it has requested meaning it returns a future whilst another side of the program continues to run.


#[allow(unused_imports)]
use tokio::time::{Duration, sleep};

// First async function: simulates fetching data
async fn fetch_user_data(user_id: u32) -> String {
println!("Fetching data for user {}...", user_id);
sleep(Duration::from_millis(500)).await; // Simulate network delay
format!("User Data for ID: {}", user_id)
}

// Second async function: simulates processing data
async fn process_data(data: String) -> String {
println!("Processing: '{}'...", data);
sleep(Duration::from_millis(300)).await; // Simulate work being done
data.to_uppercase()
}

#[tokio::main]
async fn main() {
println!("--- Async Workflow Started ---");

// Execute the async functions sequentially using .await
let user_data = fetch_user_data(42).await;
let processed_result = process_data(user_data).await;

println!("Final Result: {}", processed_result);
println!("--- Async Workflow Completed ---");

Generalizing Multi-thread programs Runtime and executors.

Async is a programming pattern in the concurrent(multi-thread ) programming model. There is also another programming pattern which is not Async but just ordinary multi-thread program.

Get edbert kwesi’s stories in your inbox

Join Medium for free to get updates from this writer.

To identify Async programs any function having async and await is asynchronous program, in-addition an Async function has a future Trait in Rust meaning a computation from that function can hang on for a while called Pending in Rust and can also be carried out immediately called Poll.

Every Async program has a time, meaning some duration to sleep. Imagine a numerous Async threads, which are to run how would that happen.? Would it happen sequentially or randomly. The Runtime Executor in Tokio Rust is a sort of scheduler , a form of queuing algorithm called WORK-STEALING FIRST-IN FIRST-OUT algorithm that regroups all pending computations and activate them again. it then does this with a timer.

Press enter or click to view image in full size
output struct of a future trait

With this you can implement a future for any function , meaning that function will now be able to either Poll or Pending. For example the code below now has a Future implemented for socket Read meaning socket read can return ready(Poll) or Pending

Press enter or click to view image in full size
implementing a Future for socket Read.

The tokio crate is specifically designed for runtime computations in async mode.This crate has an inbuilt executor that reschedules pending threads from the future struct for re-computation. To use the tokio crate successfully you can apply the same programming techniques used in concurrency, such as spawning a task, joining task threads together.

Press enter or click to view image in full size
Spawning threads in tokio

What’s Next on upcoming post.

More examples on implementing a future trait for any function , how the code looks like.

Understanding the concept of the Runtime executor.

--

--