Skip to content

Rust macros for container initialization and iterator literals

License

Notifications You must be signed in to change notification settings

kmcallister/literator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Rust macros for container initialization and iterator literals

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages