0Pricing
Learn Rust Coding · Lesson

Introduction to Async/Await

Learn the fundamentals of `async` functions and `await` expressions for writing asynchronous, non-blocking code.

Introduction to Async/Await is a free Learn Rust Coding lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Asynchronous Programming?

In programming, tasks often involve waiting. This could be waiting for a file to load, a network request to complete, or a database query to return results.

Traditionally, programs block during these waits, meaning they pause all other operations until the task finishes. This can make applications slow and unresponsive.

When Code Waits...

Consider this simple example of a blocking operation. The program will pause for 2 seconds before printing the second message.

Try running it:

use std::thread;
use std::time::Duration;

fn main() {
  println!("Starting a blocking task...");
  thread::sleep(Duration::from_secs(2)); // This blocks!
  println!("Blocking task finished!");
}

The Cost of Waiting

In the previous example, the entire program waited. If this were a web server, it would mean that while one user's request was waiting for a database, no other users could be served!

Asynchronous programming allows your program to start a long-running task, then move on to other work instead of waiting. When the long task is ready, the program can pick it up.

Rust's Async Approach

Rust provides powerful language features for asynchronous programming: the async and await keywords.

  • async functions are special functions that can be paused and resumed.
  • .await is used inside an async function to wait for another async task to complete without blocking the entire program.

Defining `async` Functions

You declare an asynchronous function by adding the async keyword before fn. An async function always returns a Future.

A Future is a value that might not be ready yet. It represents a computation that will eventually produce a result.

async fn my_async_task() -> u32 {
  // This task will eventually return 42
  42
}

Pausing with `.await`

The .await keyword is what makes asynchronous programming non-blocking. When you .await a Future, your async function will:

  • Pause its own execution.
  • Allow the program to do other work.
  • Resume once the awaited Future is ready.

It's like pressing a pause button on your current task while another task finishes.

Running Async Code: Executors

An async function returns a Future, but it doesn't run the code inside immediately. Futures are lazy.

To actually run an async function and execute its Future, you need an executor. For simple examples, we can use futures::executor::block_on, which runs a Future to completion on the current thread.

Your First `async`/`await` Program

Here's how async, await, and a simple executor work together. Notice how block_on is used in main to start the async process.

This example simulates an async delay without blocking the main function's thread (conceptually; block_on itself blocks until the future is done, but the async task itself yields).

use futures::executor::block_on;
use std::time::Duration;

async fn learn_and_wait() {
  println!("Learning asynchronously...");
  // Simulate an async operation that takes time
  // In a real app, this would be an I/O op
  tokio::time::sleep(Duration::from_secs(1)).await;
  println!("Finished learning!");
}

fn main() {
  println!("Starting main function.");
  block_on(learn_and_wait());
  println!("Main function finished.");
}

Understanding the `Future` Trait

When you write async fn my_func(), Rust actually transforms it into a state machine that implements the Future trait.

The Future trait has one main method, poll, which an executor repeatedly calls to check if the task is ready to make progress. When a Future is .awaited, it tells the executor to poll another Future until it's done.

Quick Check: Async/Await Basics

You've learned the basics of async and await. Let's see if you can identify the key roles.

Recap & Next Steps

Great job! You've taken your first steps into asynchronous Rust programming.

  • We saw that synchronous code blocks, making programs wait.
  • Asynchronous code allows tasks to run concurrently without blocking.
  • The async keyword declares an asynchronous function, which returns a Future.
  • The .await keyword pauses an async function until a Future is ready.
  • An executor (like block_on) is needed to run Futures.

Next, we'll dive into Tokio, a powerful runtime and ecosystem for building real-world asynchronous applications in Rust!

Frequently asked questions

Is the “Introduction to Async/Await” lesson free?

Yes — the full text of “Introduction to Async/Await” is free to read here on the web, and the Learn Rust Coding course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “Introduction to Async/Await”?

Learn the fundamentals of `async` functions and `await` expressions for writing asynchronous, non-blocking code. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Introduction to Async/Await” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to Async/Await
  2. Building Async Applications with Tokio
  3. Working with Futures and Tasks
← Back to Learn Rust Coding