macro_rules! heap_array {
    [] => { ... };
    [$($x:expr),+] => { ... };
    [$elem:expr; 0] => { ... };
    [$elem:expr; $n:expr] => { ... };
}
Expand description

Creates a HeapArray containing the arguments.

heap_array! allows HeapArray’s to be defined with the same syntax as array expressions. There are two forms of this macro:

  • Create a HeapArray containing a given list of elements:
let v = heap_array![1, 2, 3];
assert_eq!(v[0], 1);
assert_eq!(v[1], 2);
assert_eq!(v[2], 3);
  • Create a HeapArray from a given element and size:
let v = heap_array![1; 3];
assert_eq!(*v, [1, 1, 1]);

Note that unlike array expressions this syntax supports all elements which implement Clone and the number of elements doesn’t have to be a constant.

This will use clone to duplicate an expression, so one should be careful using this with types having a nonstandard Clone implementation. For example, heap_array![Rc::new(1); 5] will create a heap-array of five references to the same boxed integer value, not five references pointing to independently boxed integers.

Also, note that heap_array![expr; 0] is allowed, and produces an empty HeapArray. This will still evaluate expr, however, and immediately drop the resulting value, so be mindful of side effects.