How to Use Shared State in Async Rust (DashMap, etc.)

Use the DashMap crate to safely share mutable state across threads in async Rust with minimal boilerplate.

Use DashMap from the dashmap crate to share mutable state across threads safely without explicit locks. Add dashmap to your Cargo.toml dependencies and wrap your data in DashMap to allow concurrent reads and writes.

use dashmap::DashMap;

let map: DashMap<i32, String> = DashMap::new();
map.insert(1, "one".to_string());
println!("{:?}", map.get(&1));