-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.rs
65 lines (58 loc) · 1.73 KB
/
stack.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
*
* Stacks
* Stacks are data types that follows Last In First Out principle.
* You can imaging stack as a stack of plates, or a packet of cookies.
* The last item added in the stack is going to be picked to be used.
*
* The main purpose of this program is only to understand how stack works since
* stacks are used everywhere in the low level. We will be using an example that
* behaves as s stack, but in the memory, it might take either heap or stack.
*
* Rust's Vector data type already works as a stack, but we are wrapping it to
* a custom structure named `Stack` to make the concept clearer.
*/
struct Stack<T> {
content: Vec<T>, // container for items to store in the stack
}
impl<T> Stack<T> {
fn new() -> Self {
Self {
content: Vec::new(),
}
}
fn pop(&mut self) -> Option<T> {
return self.content.pop();
}
fn push(&mut self, item: T) {
self.content.push(item)
}
}
/// To run / test, please run the following command in your terminal:
/// * cargo run --bin stack
/// * cargo test --bin stack
///
fn main() {
let mut stack: Stack<i32> = Stack::new();
stack.push(1);
stack.push(2);
println!("Last item: {}", stack.pop().unwrap()); // 2
println!("Second Last item: {}", stack.pop().unwrap()); // 1
}
#[cfg(test)]
mod test {
use crate::Stack;
#[test]
fn test_stack() {
let mut stack: Stack<u32> = Stack::new();
stack.push(0);
stack.push(1);
assert_eq!(stack.pop().unwrap(), 1); // last item
assert_eq!(stack.pop().unwrap(), 0); // last item
}
#[test]
fn test_empty_stack() {
let mut stack: Stack<u32> = Stack::new();
assert_eq!(stack.pop(), None) // no items to pop
}
}