std_macro_extensions/hash_map/
macro.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/// Creates a new `HashMap` instance.
///
/// This macro can be used in two forms:
/// - Without arguments, it creates an empty `HashMap`.
/// - With key-value pairs, it creates a `HashMap` and inserts the provided pairs into it.
///
/// # Examples
/// Creating an empty `HashMap`:
/// ```
/// use std_macro_extensions::*;
/// let my_map: HashMap<&str, i32> = hash_map!();
/// ```
///
/// Creating a `HashMap` with key-value pairs:
/// ```
/// use std_macro_extensions::*;
/// let my_map = hash_map!("a" => 1, "b" => 2);
/// ```
#[macro_export]
macro_rules! hash_map {
    () => {
        std::collections::HashMap::new()
    };
    ($($key:expr => $val:expr),*) => {{
        let mut map = std::collections::HashMap::new();
        $( map.insert($key, $val); )*
        map
    }};
}