1 个不稳定版本
0.3.5 | 2024年4月29日 |
---|---|
0.3.4 |
|
0.3.2 |
|
0.2.1 |
|
0.1.0 |
|
在 数据结构 中排名第 1764
每月下载量 72
76KB
725 行
astack.rs
astack 提供了一个具有固定容量且能够快速执行 LIFO 操作的 Stack
数据结构。
该包既适用于 std 环境也适用于非 std 环境。它甚至不需要 alloc
包。
Stack
不允许索引操作,但提供了三种与栈顶进行操作的方法
- 检查方法,如
pop
和push
:执行操作并返回一个Option
/Result
如果操作不成功。 - 恐慌方法,如
pop_panicking
和push_panicking
:执行操作,如果操作不成功则引发恐慌。 - 未检查方法,如
pop_unchecked
和push_unchecked
:执行操作,如果操作不成功则引发未定义的行为。
示例
use astack::stack;
fn main() {
// Create an empty stack of i32 with can hold up to 8 numbers.
let mut stack = stack![i32; 8];
// Push an item to the end of the stack.
// Returns Err if the stack is full.
stack.push(10).unwrap();
stack.push(20).unwrap();
// Pop the top of the stack. Returns None if the stack is empty.
assert_eq!(stack.pop(), Some(20));
// Get a reference to TOS with Stack::tos.
// Get a mutable reference to TOS with Stack::tos_mut.
assert_eq!(stack.tos(), Some(&10));
// Remove all the elements from the stack
stack.clear();
assert!(stack.is_empty());
}
许可证
许可协议下 MIT 许可证 或 Apache 许可证 2.0