dashmap 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

DashMap for Rust: Complete Guide to Concurrent Hash Maps

\n\n
\n

1. What is it and why is it needed

\n

\n DashMap is a high-performance, concurrent hash map for the Rust language, designed for use in multithreaded applications. The Rust standard library has HashMap, but it is not thread-safe. If you try to use HashMap from multiple threads without external synchronization (e.g., Mutex or RwLock), the Rust compiler will throw an error. DashMap solves this problem by providing a ready-made data structure that can be safely used simultaneously from many threads without needing to wrap it in locks manually.\n

\n

\n Why is this needed? In modern multithreaded applications, there is often a need to have shared state accessed by multiple threads. A classic example is a cache, a web server processing requests in a thread pool, or an in-memory database. Using Mutex<HashMap> can become a bottleneck, because Mutex locks the entire map, even if different threads are working with different keys. DashMap uses a technique called "sharding": the internal hash table is divided into several independent segments (shards), each protected by its own RwLock. This allows simultaneous read and write operations on different segments without mutual blocking, providing a significant performance boost on multi-core processors.\n

\n

\n DashMap is not just a thread-safe wrapper. It provides a rich API, including iterators, conditional operations (compare-and-swap), insert with return, and much more. It is designed to be as ergonomic and intuitive as possible for developers accustomed to the standard HashMap. It is one of the most popular and mature libraries in the Rust ecosystem for working with concurrent data.\n

\n
\n\n
\n

2. Installation

\n

To add DashMap to your Rust project, use the cargo package manager. Run the following command in the root directory of your project:

\n
cargo add dashmap
\n

This command will add the latest version of the library to your Cargo.toml file. If you prefer to do it manually, add the following line to the [dependencies] section:

\n
[dependencies]\ndashmap = "6.1"
\n

At the time of writing, the latest stable version is 6.1. It is recommended to use the current version, as the library is actively developed.

\n
\n\n
\n

3. Quick Start

\n

Below is a minimal working example demonstrating creating a DashMap, inserting, reading, and deleting data.

\n
use dashmap::DashMap;\n\nfn main() {\n    // Create a new empty hash map\n    let map = DashMap::new();\n\n    // Insert key-value pairs\n    map.insert("key1", 10);\n    map.insert("key2", 20);\n\n    // Read a value by key\n    if let Some(value) = map.get("key1") {\n        println!("Value for 'key1': {}", *value);\n    }\n\n    // Update a value\n    map.insert("key1", 100);\n\n    // Remove an element\n    map.remove("key2");\n\n    // Check the length\n    println!("Number of elements: {}", map.len());\n}
\n

This example compiles and runs without any additional locks, which is a key advantage of DashMap.

\n
\n\n
\n

4. Main Methods

\n

Let's look at 8 key methods

Recommendations