Skip to main content
tokio-stream provides utilities to work with Stream and Tokio. A Stream is an asynchronous sequence of values - think of it as an asynchronous version of the standard library’s Iterator trait.

Installation

Add tokio-stream to your Cargo.toml:
Cargo.toml

With All Features

Cargo.toml

Features

time

Time-based stream utilities (enabled by default)

net

Network stream wrappers for TCP/Unix sockets

io-util

I/O utility stream wrappers

fs

File system stream wrappers

sync

Synchronization primitive stream wrappers

signal

OS signal stream wrappers

Basic Usage

Iterating Over a Stream

You cannot use for in syntax with streams. Instead, use while let with the next() method:
Always bring StreamExt into scope to access stream methods like next(), filter(), map(), etc.

Creating Streams

From an Iterator

Single Value Stream

Empty Stream

Pending Stream

A stream that never yields any values:

Stream Extension Methods

The StreamExt trait provides numerous combinator methods:

Filtering and Mapping

Filter Map

Take and Skip

Take While and Skip While

Fold and Collect

All and Any

Merging Streams

Chaining Streams

Then - Async Map

Time-Based Utilities

Requires the time feature (enabled by default).

Timeout

Add a timeout to each item in the stream:

Throttle

Limit the rate at which items are produced:

Chunks with Timeout

Group items into chunks, yielding when chunk size or timeout is reached:

Wrappers

The wrappers module provides Stream implementations for Tokio types.

Interval Stream

Requires the time feature.

MPSC Channel Stream

Requires the sync feature.

Broadcast Channel Stream

Requires the sync feature.

Watch Channel Stream

Requires the sync feature.

TCP Listener Stream

Requires the net feature.

ReadDir Stream

Requires the fs feature.

StreamMap

Manage multiple keyed streams concurrently:

Resources

API Documentation

Complete API reference on docs.rs

Streams Tutorial

In-depth tutorial on the Tokio website

GitHub Repository

View source code and examples

futures StreamExt

Alternative StreamExt from the futures crate