Complete the code to initialize an empty stack for integers.
let mut stack: Vec<i32> = Vec::[1]();with_capacity without specifying capacity.from which requires an initial collection.We use Vec::new() to create an empty vector which acts as a stack.
Complete the code to push an element onto the stack.
stack.[1](num);pop which removes elements.append which adds multiple elements.Use push to add an element to the top of the stack.
Fix the error in the condition to check if the stack is not empty.
while !stack.[1]().is_empty() && stack.last().unwrap() < num {
pop() which removes elements.last() which returns an element, not a boolean.Use stack.len() to check if the stack is not empty.
Fill both blanks to assign the next greater element and push the current index.
result[i] = if stack.is_empty() [1] else [2]; stack.push(i);
stack.pop() which removes the element.If the stack is empty, no greater element exists, so assign -1. Otherwise, assign the index at the top of the stack.
Fill all three blanks to complete the loop that finds the next greater element for each number.
for [1] in (0..nums.len()).rev() { let num = nums[[2]]; while !stack.is_empty() && nums[*stack.last().unwrap()] <= num { stack.pop(); } result[[3]] = if stack.is_empty() { -1 } else { nums[*stack.last().unwrap()] }; stack.push([1]); }
num as an index which is incorrect.The loop variable i is used to index the array and result vector consistently.
