[−][src]Macro col_proc_macros::vector
Creates a vector of type Vector<T>
from the elements.
Examples
Initialize a vector with some numbers:
use col_proc_macros::vector; type Vector<T> = Vec<T>; let vec = vector![1, 2, 3,]; assert_eq!(vec, [1, 2, 3]);
Initialize a vector with a large number of repeated elements:
// writing an equivalent code with normal syntax would be insane let vec = vector![1; 10000]; assert_eq!(vec, &[1; 10000][..]);
Initialize a multi-dimensional vector:
let vec = vector![ vector![1, 2, 3], vector![4, 5, 6], vector![7, 8, 9]]; assert_eq!(vec[1][2], 6);
Rapidly annotate the type of the vector elements in any context with <elements>: <type>
syntax:
let vector = vector![Default::default(), Default::default(): i32,]; assert_eq!(vector, [0, 0]);
The type must go at the last element or will be parsed as-is and rustc will fail with the message
type ascription is experimental
,as such,this syntax it's influenced by that language feature
being the only equivalent which have the same result.
The Vector<T>
type should have the following methods:
with_capacity
to allocate a vector with so capacity as elements.as_mut_ptr
to move the elements to the vector from an array.set_len
to set the length.
It's worth to mention that,like vec!
,the resulting length must not overflow the stack because
the macro declares an array and then moves the elements to the vector.