macro_rules! array_stack {
    () => { ... };
    ($($elem:expr),*) => { ... };
    ($elem:expr; $n:expr) => { ... };
}
Expand description

Creates a ArrayStack containing the arguments.

array_stack! allows ArrayStacks to be defined with the same syntax as array expressions. There are three forms of this macro:

use trait_based_collection::{prelude::*, ArrayStack, array_stack};

let mut c = array_stack![];
c.add(1);
assert_eq!(c.remove(), Some(1));
  • Create a ArrayStack containing a given list of elements:
use trait_based_collection::{prelude::*, ArrayStack, array_stack};
use std::iter::zip;

let c1 = array_stack![1, 2, 3];
let c2 = ArrayStack::from_iter([1, 2, 3].into_iter());
for (actual, expected) in zip(c1, c2) {
    assert_eq!(actual, expected);
}
  • Create a ArrayStack from a given element and size:
use trait_based_collection::{prelude::*, ArrayStack, array_stack};
use std::iter::zip;

let c1 = array_stack![1; 3];
let c2 = ArrayStack::from_iter([1; 3].into_iter());
for (actual, expected) in zip(c1, c2) {
    assert_eq!(actual, expected);
}

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, array_stack![Rc::new(1); 5] will create a vector of five references to the same boxed integer value, not five references pointing to independently boxed integers.