grabbag_macros::collect! [] [src]

macro_rules! collect {
    [] => { ... };
    [into $col_ty:ty] => { ... };
    [into $col_ty:ty:] => { ... };
    [into $col_ty:ty: $v0:expr] => { ... };
    [into $col_ty:ty: $v0:expr, $($vs:expr),* $(,)*] => { ... };
    [$($vs:expr),+] => { ... };
    [into $col_ty:ty: $($ks:expr => $vs:expr),+] => { ... };
    [$($ks:expr => $vs:expr),+] => { ... };
}

This macro provides a way to initialise any container for which there is a FromIterator implementation. It allows for both sequence and map syntax to be used, as well as inline type ascription for the result.

For example:

// Initialise an empty collection.
let a: Vec<int> = collect![];
let b: HashMap<String, bool> = collect![];

// Initialise a sequence.
let c: String = collect!['a', 'b', 'c'];

// Initialise a sequence with a type constraint.
let d = collect![into Vec<_>: 0, 1, 2];

// Initialise a map collection.
let e: VecMap<&str> = collect![1 => "one", 2 => "two", 3 => "many", 4 => "lots"];

// Initialise a map with a type constraint.
let f: HashMap<_, u8> = collect![into HashMap<int, _>: 42 => 0, -11 => 2];