fn main() {
let mut arr = vec![1, 2, 3];
arr.push(4);
arr.pop();
arr.push(5);
println!("{:?}", arr);
let mut stack = Vec::new();
stack.push(1);
stack.push(2);
stack.push(3);
stack.pop();
stack.push(5);
println!("{:?}", stack);
}The array starts as [1, 2, 3]. We push 4, making it [1, 2, 3, 4]. Then pop removes 4, back to [1, 2, 3]. Then push 5 makes it [1, 2, 3, 5].
The stack is built by pushing 1, 2, 3, then popping removes 3, then pushing 5 results in [1, 2, 5].
Stack abstraction limits operations to push and pop only, preventing accidental misuse like inserting or removing elements from the middle, which can happen with arrays.
fn main() {
let mut arr = vec![1, 2, 3];
arr.pop();
arr.pop();
arr.pop();
arr.pop(); // extra pop
println!("{:?}", arr);
}Calling pop on an empty vector returns None safely in Rust, so no panic occurs. But if code unwraps the pop result without checking, panic happens. Here, pop is called without unwrap, so no panic.
However, since the code just calls pop without unwrap, no panic occurs and prints empty vector.
fn main() {
let mut arr = vec![10, 20];
arr.push(30);
arr.remove(0);
arr.push(40);
println!("{:?}", arr);
let mut stack = Vec::new();
stack.push(10);
stack.push(20);
stack.push(30);
// stack.remove(0); // invalid for stack abstraction
stack.pop();
stack.push(40);
println!("{:?}", stack);
}Array removes first element (10), so becomes [20, 30], then pushes 40 → [20, 30, 40].
Stack pushes 10, 20, 30, pops 30, then pushes 40 → [10, 20, 40].
Stack abstraction hides the underlying data structure and only exposes push/pop, preventing accidental misuse and making the code's purpose clear.
