rustHow to create a Rust HashMap?
Creating a Rust HashMap is easy and straightforward.
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("key1", "value1");
map.insert("key2", "value2");
The output of the above code will be:
()
Code explanation
use std::collections::HashMap;- This imports the HashMap module from the standard library.let mut map = HashMap::new();- This creates a new, empty HashMap.map.insert("key1", "value1");- This inserts a key-value pair into the HashMap.map.insert("key2", "value2");- This inserts another key-value pair into the HashMap.
Helpful links
Related
- How to create a HashMap of structs in Rust?
- How to create a new Rust HashMap with a specific type?
- How to sort the keys in a Rust HashMap?
- How to convert the keys of a Rust HashMap to a vector?
- How to convert a Rust HashMap to a JSON string?
- How to compare two Rust HashMaps?
- How to use a custom hash function with a Rust HashMap?
- How to build a Rust HashMap from an iterator?
- How to convert a Rust HashMap to a BTreeMap?
- How to convert a Rust HashMap to JSON?
More of Rust
- How to match whitespace with a regex in Rust?
- How to match a URL with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to replace all matches using Rust regex?
- How to sort the keys in a Rust HashMap?
- How to split a string with Rust regex?
- How to get a capture group using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to match the end of a line in a Rust regex?
- How to use regex with bytes in Rust?
See more codes...