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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/// Create a `BTreeMap` containing the arguments
/// 
/// `btreemap!` allows `BTreeMap`s to be constructed using minimal syntax:
/// 
/// - Create a `BTreeMap` using a list of `key => value` pairs:
/// 
/// ```
/// # #[macro_use] extern crate collection_macros;
/// # fn main() {
/// let m = btreemap!{
///     1 => "foo",
///     2 => "bar",
///     3 => "baz",
///     5 => "quux",
/// };
/// assert_eq!(m.get(&1), Some(&"foo"));
/// assert_eq!(m.get(&2), Some(&"bar"));
/// assert_eq!(m.get(&3), Some(&"baz"));
/// assert_eq!(m.get(&5), Some(&"quux"));
/// # }
/// ```
/// 
#[macro_export]
macro_rules! btreemap {
    ( $($x:expr => $y:expr),* ) => ({
        use std::collections::BTreeMap;
        let mut temp_map = BTreeMap::new();
        $(
            temp_map.insert($x, $y);
        )*
        temp_map
    });
    ( $($x:expr => $y:expr,)* ) => (
        btreemap!{$($x => $y),*}
    );
}

#[cfg(test)] mod tests {
    use std::collections::BTreeMap;

    #[test]
    fn test_btreemap() {
        let map = btreemap! {
            1 => "hello",
            3 => "blah",
        };

        let mut should_be = BTreeMap::new();
        should_be.insert(1, "hello");
        should_be.insert(3, "blah");

        assert_eq!(map, should_be);

    }
}