All Projects → kmcallister → literator

kmcallister / literator

Licence: other
Rust macros for container initialization and iterator literals

Programming Languages

rust
11053 projects

Container initialization

This library provides a macro for initializing any container implementing FromIterator.

#[macro_use] extern crate literator;

use std::collections::HashMap;

fn main() {
    let v: Vec<_> = container![1, 2, 3];
    assert_eq!(&v, &[1, 2, 3]);

    let h: HashMap<_, _> = container! {
        (1, 'x'),
        (2, 'y'),
    };
    assert_eq!(h[1], 'x');
    assert_eq!(h[2], 'y');
    assert_eq!(h.len(), 2);
}

A Perl-ish sugar for pairs is also available:

let h: HashMap<_, _> = container! {
    1 => 'x',
    2 => 'y',
};

Iterator literals

container! is built on top of an "iterator literal" macro:

let mut it = literator!['x', 'y', 'z'];
assert_eq!(it.next(), Some('x'));
assert_eq!(it.next(), Some('y'));

literator! works without heap allocation. The elements are moved into a fixed-size array and then back out during iteration. Currently it supports up to 32 entries. Once variadic generics are available, there should be no limit.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].