Module cl_structures::stack
source · Expand description
A contiguous collection with constant capacity.
Since the capacity of a Stack may be known at compile time, it may live on the call stack.
§Examples
Unlike a Vec, the Stack doesn’t grow when it reaches capacity.
ⓘ
let mut v = stack![1];
v.push("This should work");
v.push("This will panic!");
To get around this limitation, the methods try_push and try_insert are provided:
let mut v = stack![1];
v.push("This should work");
v.try_push("This should produce an err").unwrap_err();
As the name suggests, a Stack enforces a stack discipline:
let mut v = stack![100];
assert_eq!(100, v.capacity());
assert_eq!(0, v.len());
// Elements are pushed one at a time onto the stack
v.push("foo");
v.push("bar");
assert_eq!(2, v.len());
// The stack can be used anywhere a slice is expected
assert_eq!(Some(&"foo"), v.get(0));
assert_eq!(Some(&"bar"), v.last());
// Elements are popped from the stack in reverse order
assert_eq!(Some("bar"), v.pop());
assert_eq!(Some("foo"), v.pop());
assert_eq!(None, v.pop());
Macros§
- Creates a
stack
containing the arguments
Structs§
- A contiguous collection with constant capacity