Module futures
Async programming primitives and utilities.
futures provides fundamental traits and utilities for asynchronous programming in Rust.
It defines the core Future trait and provides essential async utilities
that work with both the standard library and async runtimes like Tokio.
The crate includes several key modules:
future for working with individual futures,
stream for handling asynchronous streams of data,
sink for asynchronous data consumption,
executor for running futures to completion,
and channel for async communication primitives.
Key traits include Future for asynchronous computations,
Stream for asynchronous iterators,
and Sink for asynchronous data receivers.
The library also provides combinators for chaining and transforming futures.
The channel module provides essential async communication building blocks:
mpsc channels for multiple-producer, single-consumer communication,
and oneshot channels for one-time value passing between tasks.
These are runtime-agnostic and work across different async executors.
This crate serves as the foundation for async Rust, providing compatibility between different async runtimes and offering low-level building blocks for async applications.
Examples
Working with futures and combinators:
use ;
// Create simple futures
let fut1 = ready;
let fut2 = ready;
// Combine futures
let combined = join;
// Execute the future
let = block_on;
assert_eq!;
assert_eq!;
Using async streams:
use ;
// Create a stream of numbers
let stream = iter;
// Transform the stream
let doubled_stream = stream.map;
let doubled: = block_on;
assert_eq!;
Using mpsc channels for async communication:
use ;
let = ;
// Send some values
block_on;
// Receive values
let received: = block_on;
assert_eq!;
Using oneshot channels for single-value communication:
use ;
let = ;
// Send a value
sender.send.unwrap;
// Receive the value
let message = block_on.unwrap;
assert_eq!;