Complete the code to peek the front element of the queue using the correct method.
let front = queue.[1]();The front method returns a reference to the front element of the queue without removing it.
Complete the code to safely get the front element of the queue and handle the case when the queue is empty.
if let Some(front) = queue.[1]() { println!("Front element: {}", front); } else { println!("Queue is empty"); }
The front method returns an Option with a reference to the front element, allowing safe handling of empty queues.
Fix the error in the code to correctly print the front element of the queue.
println!("Front element: {}", queue.[1]().unwrap());
The front method returns an Option, so calling unwrap() on it safely gets the front element if it exists.
Fill in the blank to create a function that returns the front element of the queue or None if empty.
fn peek_front(queue: &VecDeque<i32>) -> Option<&i32> {
queue.[1]()
}
let front = peek_front(&queue);
match front {
Some(value) => println!("Front: {}", value),
None => println!("Queue empty"),
}The front method returns an Option with a reference to the front element without removing it, perfect for this function.
Fill all three blanks to create a function that prints the front element or a message if the queue is empty.
fn print_front(queue: &VecDeque<i32>) {
match queue.[1]() {
Some([2]) => println!("Front element: {}", [3]),
None => println!("Queue is empty"),
}
}The front method returns an Option with a reference to the front element. The variable name can be any valid identifier, here value is used consistently.
