37 lines
823 B
Rust
Raw Normal View History

2024-06-22 17:19:55 +02:00
#[macro_export]
macro_rules! map {
() => { std::collections::HashMap::new() };
($($k:expr => $v:expr),* $(,)?) => {
{
let mut temp_map = std::collections::HashMap::new();
$(
temp_map.insert($k, $v);
)*
temp_map
}
};
}
#[cfg(test)]
2024-06-22 17:19:55 +02:00
mod tests {
use std::collections::HashMap;
#[test]
fn test_empty_map() {
let map: HashMap<usize, usize> = map!();
assert_eq!(map.len(), 0);
}
#[test]
fn test_map() {
let map = map! {
"one" => 1,
"two" => 2,
"three" => 3,
};
assert_eq!(map.len(), 3);
assert_eq!(map.get("one"), Some(&1));
assert_eq!(map.get("two"), Some(&2));
assert_eq!(map.get("three"), Some(&3));
}
}