Use the dashmap crate to create a concurrent, thread-safe HashMap in Rust for safe multi-threaded data access.
Add dashmap to your Cargo.toml dependencies and import it to create a thread-safe hash map that allows concurrent reads and writes without external locks.
[dependencies]
dashmap = "6"
use dashmap::DashMap;
fn main() {
let map: DashMap<String, i32> = DashMap::new();
map.insert("key", 42);
if let Some(value) = map.get("key") {
println!("Value: {}", *value);
}
}
DashMap is a thread-safe version of Rust's standard HashMap designed for high-performance concurrent applications. It allows multiple threads to read and write data simultaneously without causing crashes or data corruption. Think of it as a shared notebook where multiple people can write notes at the same time without stepping on each other's toes.