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

Creates a Stack containing the arguments.

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

use trait_based_collection::{prelude::*, Stack, stack};

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

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

let c1 = stack![1; 3];
let c2 = Stack::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, 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.