Tips for Web

Rust Async: Future vs Stream Explained Simply

rust

Rust’s asynchronous system is fast, safe, and powerful—but understanding the difference between Future and Stream can be confusing when you’re just getting started.

In this post, we’ll break it down clearly and simply, with examples, use cases, and SEO-friendly insights that will help you write scalable async Rust applications.

What Is a Future in Rust?

A Future in Rust represents a single asynchronous computation that will eventually produce a single result (or error). You can think of it like a promise in JavaScript or a one-time callback.

Example of a Rust Future

async fn fetch_user_id() -> u32 {
    42
}

#[tokio::main]
async fn main() {
    let user_id = fetch_user_id().await;
    println!("User ID: {}", user_id);
}

Here, .await tells Rust to “pause” and wait for the future to complete.

What Is a Stream in Rust?

A Stream is like an async iterator — it produces multiple values over time. Think of a stream as a series of Futures that yield values one-by-one.

Example of a Rust Stream

use tokio_stream::StreamExt;

#[tokio::main]
async fn main() {
    let numbers = tokio_stream::iter(vec![1, 2, 3]);

    tokio::pin!(numbers);

    while let Some(num) = numbers.next().await {
        println!("Received: {}", num);
    }
}

Each call to .next().await gives you one item from the stream.

Future vs Stream in Rust

FeatureFutureStream
YieldsOne valueMany values over time
Async API.await.next().await
TraitFutureStream (from futures crate)
Example useHTTP request, sleepWebSocket, file lines, events
Similar toPromise or Result<T>Iterator<Item>

When to Use a Future

Examples:

When to Use a Stream

Use Stream when:

Libraries for Async in Rust

LibraryPurpose
tokioAsync runtime for Rust (event loop)
tokio-streamStream utilities for use with Tokio
futuresCore traits and utilities for async Rust
async-streamMacro-based streams (generator-like)

Summary

QuestionFutureStream
How many values does it produce?OneMany
How to consume it?.await.next().await
Used forSingle async resultAsync sequence
Synchronous analogyResult<T>Iterator<Item>
Rust modulestd::future::Futurefutures::stream::Stream
Exit mobile version