#macro #hash-map #literals #vector #spread

velcro

Convenience macros for initializing vectors, hash maps and other Rust collections

14 releases

0.5.4 Jan 1, 2023
0.5.3 Jan 15, 2021
0.5.2 Dec 20, 2020
0.4.4 Dec 9, 2020
0.1.2 Nov 25, 2020

#243 in Data structures

Download history 2735/week @ 2024-07-22 3208/week @ 2024-07-29 4360/week @ 2024-08-05 2925/week @ 2024-08-12 2630/week @ 2024-08-19 3247/week @ 2024-08-26 3219/week @ 2024-09-02 3472/week @ 2024-09-09 3462/week @ 2024-09-16 3836/week @ 2024-09-23 2642/week @ 2024-09-30 3191/week @ 2024-10-07 2599/week @ 2024-10-14 2908/week @ 2024-10-21 2974/week @ 2024-10-28 2123/week @ 2024-11-04

10,671 downloads per month
Used in 33 crates (16 directly)

Unlicense OR MIT

18KB

Velcro

Build Status

A set of macros for conveniently initializing collections from Rust's std and iterators. All of the macros support the unary .. operator which "spreads" the values of another collection or iterator.

velcro::vec! is a drop-in replacement for std::vec!. All functionality of the std macro is supported without overhead, but it also supports spreading values with the .. operator.

Examples

use velcro::{hash_map, iter, vec};

assert_eq!(vec![0, 1, ..(2..7)], vec![0, 1, 2, 3, 4, 5, 6]);

let other = vec![3, 4, 5];
assert_eq!(vec![0, 1, 2, ..&other, 6], vec![0, 1, 2, 3, 4, 5, 6]);

let whitespace = iter![' ', '\t', '\r', '\n'];
let map = hash_map! {
    ..('0'..='9'): "digit",
    ..('a'..='z'): "lower",
    ..('A'..='Z'): "upper",
    ..whitespace: "whitespace",
    '.': "punctuation",
    ',': "punctuation",
};

assert_eq!(map[&'x'], "lower");
assert_eq!(map[&'\r'], "whitespace");
assert_eq!(map[&'.'], "punctuation");

Help

For help, questions or to report an issue, please use the Github issue tracker.

Dependencies

~1.5MB
~37K SLoC