How to create a HashMap literal in Rust?
Hi! I just started learning Rust and I have a problem. I'm used to writing dictionaries in Python in one line: `my_dict = {"key": "value"}`. In JavaScript it's also simple: `let obj = {key: "value"}`. But in Rust I can't find a way to create a HashMap literally.
I'm trying to do something like this:
```rust
use std::collections::HashMap;
fn main() {
// My failing code
let mut map = HashMap::new();
map.insert("key1", "value1");
map.insert("key2", "value2");
}
```
This works, but it's very verbose. In Python I would write `{"key1": "value1", "key2": "value2"}`. How to do the same in Rust? Is there some macro or syntactic sugar?
I read the documentation but only found `HashMap::new()` and `insert()`. Maybe there is some way to use `vec
No answers yet.
Log in to leave an answer