#[allow(unused_macros)]
#[macro_export]
macro_rules! map {
( $(( $k:expr, $v:expr )),* $(,)?) => {
{
let mut m = ::std::collections::HashMap::new();
$(
m.insert($k, $v);
)*
m
}
};
( $( $k:expr => $v:expr ),* $(,)?) => {
{
let mut m = ::std::collections::HashMap::new();
$(
m.insert($k, $v);
)*
m
}
};
}
#[cfg(test)]
mod test {
mod tuple_syntax {
mod one_line {
use std::collections::HashMap;
#[test]
fn trailing_comma() {
let x = map!((1, 2), (3, 4),);
assert_eq!(x, HashMap::from([(1, 2), (3, 4)]));
}
#[test]
fn no_trailing_comma() {
let x = map!((1, 2), (3, 4));
assert_eq!(x, HashMap::from([(1, 2), (3, 4)]));
}
}
mod multiple_lines {
use std::collections::HashMap;
#[test]
fn trailing_comma() {
let x = map!(
(1, 2),
(3, 4),
);
assert_eq!(x, HashMap::from([(1, 2), (3, 4)]));
}
#[test]
fn no_trailing_comma() {
let x = map!(
(1, 2),
(3, 4)
);
assert_eq!(x, HashMap::from([(1, 2), (3, 4)]));
}
}
}
mod arrow_syntax {
mod one_line {
use std::collections::HashMap;
#[test]
fn trailing_comma() {
let x = map!(1 => 2, 3 => 4,);
assert_eq!(x, HashMap::from([(1, 2), (3, 4)]));
}
#[test]
fn no_trailing_comma() {
let x = map!(1 => 2, 3 => 4);
assert_eq!(x, HashMap::from([(1, 2), (3, 4)]));
}
}
mod multiple_lines {
use std::collections::HashMap;
#[test]
fn trailing_comma() {
let x = map!(
1 => 2,
3 => 4,
);
assert_eq!(x, HashMap::from([(1, 2), (3, 4)]));
}
#[test]
fn no_trailing_comma() {
let x = map!(
1 => 2,
3 => 4
);
assert_eq!(x, HashMap::from([(1, 2), (3, 4)]));
}
}
}
}