📖 Scenario: You are building a simple stack to manage books in a small library cart. The cart can hold only a limited number of books. You want to check if the cart is empty or full before adding or removing books.
🎯 Goal: Create a stack with a fixed size, add some books, then check if the stack is empty or full.
📋 What You'll Learn
Create a stack vector called stack with a maximum size of 3
Create a variable max_size set to 3
Add two books to the stack vector
Write code to check if the stack is empty using stack.is_empty()
Write code to check if the stack is full by comparing stack.len() with max_size
Print the results of the empty and full checks
💡 Why This Matters
🌍 Real World
Stacks are used in many real-world applications like undo features in text editors, managing function calls, and browser history.
💼 Career
Understanding stack operations and checks is fundamental for software development roles, especially those involving system programming, algorithms, and data structure design.
Progress0 / 4 steps
1
Create the stack vector
Create a vector called stack that will hold String values. Initialize it as empty.
DSA Rust
Hint
Use Vec::new() to create an empty vector. Remember to make it mutable with mut.
2
Set the maximum size of the stack
Create a variable called max_size and set it to 3.
DSA Rust
Hint
Use let max_size = 3; to create the variable.
3
Add two books to the stack
Add two books to the stack vector using push. Add "Book A" and "Book B" as String values.
DSA Rust
Hint
Use stack.push(String::from("Book A")); to add a book.
4
Check if stack is empty or full and print results
Write code to check if stack is empty using stack.is_empty() and if it is full by comparing stack.len() with max_size. Print "Stack is empty: " followed by the empty check result, and "Stack is full: " followed by the full check result.
DSA Rust
Hint
Use stack.is_empty() and stack.len() == max_size to check. Use println! to print.
Practice
(1/5)
1. What does the is_empty() method of a stack typically check?
easy
A. If the stack has more than one element
B. If the stack is full and cannot accept more elements
C. If the stack has exactly one element
D. If the stack has no elements inside
Solution
Step 1: Understand the purpose of is_empty()
This method checks whether the stack contains any elements or not.
Step 2: Interpret the meaning of empty stack
An empty stack means it has zero elements inside, so is_empty() returns true only in this case.
Final Answer:
If the stack has no elements inside -> Option D
Quick Check:
Empty stack means no elements [OK]
Hint: Empty means zero elements inside the stack [OK]
Common Mistakes:
Confusing empty with full
Thinking empty means one element
Assuming empty means stack is null
2. Which of the following is the correct Rust syntax to check if a stack named stack is full assuming it has a fixed capacity stored in capacity and current size in size?
easy
A. if stack.len() == capacity { /* full */ }
B. if stack.size == capacity { /* full */ }
C. if stack.is_full() { /* full */ }
D. if stack.size > capacity { /* full */ }
Solution
Step 1: Identify how to get current stack size
In Rust, len() method is commonly used to get the current number of elements in a collection like a stack.
Step 2: Compare current size with capacity
If stack.len() equals capacity, the stack is full.
Final Answer:
if stack.len() == capacity { /* full */ } -> Option A
Quick Check:
Use len() to check size [OK]
Hint: Use stack.len() to get current size in Rust [OK]
Common Mistakes:
Using stack.size instead of stack.len()
Checking size > capacity instead of == capacity
Assuming is_full() method exists by default
3. What will be the output of the following Rust code snippet?
A. Vec<i32> cannot be used to store stack elements
B. Missing return type in is_empty method
C. Using '=' instead of '==' for comparison in is_empty method
D. is_empty method should take mutable self parameter
Solution
Step 1: Check the comparison operator in is_empty
The code uses single '=' which is assignment, not comparison. It should use '==' to compare length with zero.
Step 2: Confirm other parts are correct
The return type bool is present, Vec<i32> is valid, and self does not need to be mutable for this check.
Final Answer:
Using '=' instead of '==' for comparison in is_empty method -> Option C
Quick Check:
Use '==' for comparison, not '=' [OK]
Hint: Use '==' to compare values, '=' is assignment [OK]
Common Mistakes:
Using '=' instead of '==' in conditions
Forgetting return type bool
Making self mutable unnecessarily
5. You have a stack with capacity 5 and current elements [10, 20, 30]. You want to write a Rust function can_push that returns true if the stack is not full and false otherwise. Which implementation is correct?