#collection #macro #initialization #initializing #literals #hash

collection_literals

Easy-to-use macros for initializing std::collections

1 stable release

1.0.1 Mar 24, 2022
1.0.0 Mar 20, 2022
1.0.0-rc1 Mar 22, 2022
1.0.0-2 Mar 21, 2022

#1737 in Rust patterns

Download history 89369/week @ 2024-07-21 85927/week @ 2024-07-28 80732/week @ 2024-08-04 107144/week @ 2024-08-11 117776/week @ 2024-08-18 111211/week @ 2024-08-25 99399/week @ 2024-09-01 135786/week @ 2024-09-08 108290/week @ 2024-09-15 134110/week @ 2024-09-22 128656/week @ 2024-09-29 166417/week @ 2024-10-06 138136/week @ 2024-10-13 160736/week @ 2024-10-20 166420/week @ 2024-10-27 167984/week @ 2024-11-03

637,809 downloads per month
Used in 259 crates (3 directly)

MIT license

11KB
64 lines

Collection Literals

Simple library for convenient collection initialization.

collection! macro

Macro for initializing collections of any type.

use collection_literals::collection;

fn main() {
    let empty_collection: HashMap<String, bool> = collection! {};
    let empty_collection = collection! { HashMap::<String, bool> };

    let hash_set: HashSet<u8> = collection! { 1, 5, 9, 12 };
    let hash_set = collection! { HashSet::<u8>; 1, 5, 9, 12 };

    let hash_map: HashMap<u64, char> = collection! {
        0 => '0',
        1 => '1',
        2 => '2',
        3 => '3',
    };
    let hash_map = collection! { HashMap::<u64, char>;
        0 => '0',
        1 => '1',
        2 => '2',
        3 => '3',
    };

    let btree_set: BTreeSet<u8> = collection! { 1, 5, 9, 12 };
    let btree_set = collection! { BTreeSet::<u8>; 1, 5, 9, 12 };

    let btree_map: BTreeMap<u64, char> = collection! {
        0 => '0',
        1 => '1',
        2 => '2',
        3 => '3',
    };
    let btree_map = collection! { BTreeMap::<u64, char>;
        0 => '0',
        1 => '1',
        2 => '2',
        3 => '3',
    };
}

hash! macro

Macro for initializing both HashMaps and HashSets. It can infer both type of collection and types of entries, but you can provide explicit type annotations.

use collection_literals::hash;

fn main() {
    let empty_hash_map: HashMap<String, bool> = hash! {};
    let empty_hash_map = hash! { map of String => bool };
    let empty_hash_map = hash! { map of String => bool {} };
    let empty_hash_map = hash! { of String => bool };
    let empty_hash_map = hash! { of String => bool {} };

    let empty_hash_set: HashSet<u8> = hash! {};
    let empty_hash_set = hash! { set of u8 };
    let empty_hash_set = hash! { set of u8 {} };
    let empty_hash_set = hash! { of u8 };
    let empty_hash_set = hash! { of u8 {} };

    let hash_map: HashMap<u64, char> = hash! {
        0 => '0',
        1 => '1',
        2 => '2',
        3 => '3',
    };
    let hash_map = hash! { map of u64 => char {
        0 => '0',
        1 => '1',
        2 => '2',
        3 => '3',
    }};
    let hash_map = hash! { of u64 => char {
        0 => '0',
        1 => '1',
        2 => '2',
        3 => '3',
    }};
    let hash_map = hash! { 
        0_u64 => '0',
        1_u64 => '1',
        2_u64 => '2',
        3_u64 => '3',
    };

    let hash_set: HashSet<u8> = hash! { 1, 2, 3 };
    let hash_set = hash! { set of u8 { 1, 2, 3 } };
    let hash_set = hash! { of u8 { 1, 2, 3 } };
    let hash_set = hash! { 1_u8, 2_u8, 3_u8 };
}

btree! macro:

Macro for initializing both BTreeMaps and BTreeSets. It can infer both type of collection and types of entries, but you can provide explicit type annotations.

use collection_literals::btree;

fn main() {
    let empty_btree_map: BTreeMap<String, bool> = btree! {};
    let empty_btree_map = btree! { map of String => bool };
    let empty_btree_map = btree! { map of String => bool {} };
    let empty_btree_map = btree! { of String => bool };
    let empty_btree_map = btree! { of String => bool {} };

    let empty_btree_set: BTreeSet<u8> = hash! {};
    let empty_btree_set = btree! { set of u8 };
    let empty_btree_set = btree! { set of u8 {} };
    let empty_btree_set = btree! { of u8 };
    let empty_btree_set = btree! { of u8 {} };

    let btree_map: BTreeMap<u64, char> = btree! {
        0 => '0',
        1 => '1',
        2 => '2',
        3 => '3',
    };
    let btree_map = btree! { map of u64 => char {
        0 => '0',
        1 => '1',
        2 => '2',
        3 => '3',
    }};
    let btree_map = btree! { of u64 => char {
        0 => '0',
        1 => '1',
        2 => '2',
        3 => '3',
    }};
    let btree_map = btree! { 
        0_u64 => '0',
        1_u64 => '1',
        2_u64 => '2',
        3_u64 => '3',
    };

    let btree_set: BTreeSet<u8> = btree! { 1, 2, 3 };
    let btree_set = btree! { set of u8 { 1, 2, 3 } };
    let btree_set = btree! { of u8 { 1, 2, 3 } };
    let btree_set = btree! { 1_u8, 2_u8, 3_u8 };
}

No runtime deps