flume for Rust: complete guide with examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course
\n

flume library for Rust: multithreading channels

\n\n
\n

1. What it is and why it's needed

\n

The flume library is a fast, safe, and ergonomic implementation of multithreaded channels for the Rust language. It provides asynchronous and synchronous queues for passing messages between threads, inspired by classic channels from Go and the crossbeam library. flume is distinguished by minimal allocations, no standard library dependencies (no_std compatibility), and excellent performance, often surpassing the standard mpsc channels from std::sync.

\n

Why is flume needed? In multithreaded Rust applications, it is often necessary to organize data exchange between threads: one thread produces data (producer), another consumes it (consumer). The standard channels from std::sync::mpsc have limitations: they only support a single consumer. flume, however, provides both single-consumer (SPSC) and multi-consumer (MPSC) channels, and also supports asynchronous programming via async/await. The library is actively used in high-load systems, web servers, and game engines due to its lightness and speed.

\n

Key features of flume: zero-cost abstractions, backpressure support (buffer size limitation), the ability to clone senders and receivers, and full compatibility with tokio and async-std. If you need reliable and fast channels for Rust, flume is an excellent choice.

\n
\n\n
\n

2. Installation

\n

To install flume, add the dependency to your project's Cargo.toml file:

\n
[dependencies]\nflume = "0.11"
\n

Then run the cargo build command to download and compile the library. If you use asynchronous features, no additional dependencies are required — flume supports async out of the box.

\n
\n\n
\n

3. Quick start — minimal working example

\n

Let's create a simple channel, send one message, and receive it:

\n
use flume;\n\nfn main() {\n    // Create an unbounded channel\n    let (tx, rx) = flume::unbounded();\n\n    // Send a message\n    tx.send("Hello from flume!").unwrap();\n\n    // Receive the message\n    let msg = rx.recv().unwrap();\n    println!("Received: {}", msg);\n}
\n

This example demonstrates basic operation: unbounded() creates a channel with no limit on the number of messages, send sends data, and recv blocks the thread until a message is received.

\n
\n\n
\n

4. Main methods/functions

\n\n

4.1. flume::unbounded()

\n

Signature: pub fn unbounded<T>() -> (Sender<T>, Receiver<T>)

\n

Creates an unbounded channel. The sender and receiver can be cloned. Messages are stored in a buffer that grows dynamically.

\n
let (tx, rx) = flume::unbounded::<String>();\ntx.send("Hello".to_string()).unwrap();\nlet msg = rx.recv().unwrap();\nprintln!("{}", msg);
\n\n

4.2. flume::bounded(cap: usize)

\n

Signature: pub fn bounded<T>(cap: usize) -> (Sender<T>, Receiver<T>)

\n

Creates a channel with a bounded buffer of the specified capacity. When the buffer is full, send will block until space becomes available.

\n
let (tx, rx) = flume::bounded(3);\nfor i in 0..3 {\n    tx.send(i).unwrap();\n}\n// The fourth send will block until space is freed\n// rx.recv();                    

Recommendations