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

collection_literals

用于初始化 std::collections 的易用宏

1 个稳定版本

1.0.1 2022年3月24日
1.0.0 2022年3月20日
1.0.0-rc1 2022年3月22日
1.0.0-2 2022年3月21日

Rust 模式 中排名 768

Download history 43492/week @ 2024-03-14 47908/week @ 2024-03-21 56706/week @ 2024-03-28 56388/week @ 2024-04-04 65871/week @ 2024-04-11 63961/week @ 2024-04-18 60703/week @ 2024-04-25 66252/week @ 2024-05-02 65496/week @ 2024-05-09 68465/week @ 2024-05-16 75631/week @ 2024-05-23 87792/week @ 2024-05-30 83913/week @ 2024-06-06 78779/week @ 2024-06-13 71335/week @ 2024-06-20 80085/week @ 2024-06-27

每月下载量 335,698
204 crate 中使用 (3 个直接使用)

MIT 许可证

11KB
64 行 代码

集合字面量

方便集合初始化的简单库。

collection!

用于初始化任何类型的集合的宏。

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!

用于初始化 HashMap 和 HashSet 的宏。它可以推断集合和条目的类型,但也可以提供显式类型注解。

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!

用于初始化 BTreeMap 和 BTreeSet 的宏。它可以推断集合和条目的类型,但也可以提供显式类型注解。

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 };
}

无运行时依赖