Enqueue values at the back and dequeue them from the front in first-in, first-out order.

Algorithm

The replay uses the same three values in every language, so this Rust DSA implementation can be compared directly with the rest of the DSA track.

front The front is the oldest value still waiting in the queue.
FIFO A queue removes values in first-in, first-out order.

Visual walkthrough

The queue keeps the oldest value at the front and adds new values at the back.

Step 1 - Enqueue 10, 20, 30

New values join at the back. The oldest value, 10, stays at the front.

Queue after three enqueues: front 10, then 20, then back 30.nextnext10front2030back

Step 2 - Dequeue removes 10

Removing from the front returns 10 and makes 20 the new front.

After one dequeue: removed is 10; front moves to 20.next10removed20front30back

Basic Implementation

basic.rs
use std::collections::VecDeque;

fn render(values: &[i32]) -> String {
    values.iter().map(|v| v.to_string()).collect::<Vec<_>>().join(" -> ")
}

fn main() {
    let mut queue = VecDeque::new();
    for value in [10, 20, 30] { queue.push_back(value); }
    let mut removed = Vec::new();
    while let Some(value) = queue.pop_front() { removed.push(value); }
    println!("{}", render(&removed));
}

Complexity

  • Time: O(1) per operation with a real queue
  • Space: O(n)

Implementation notes

  • The checked source imports std::collections::VecDeque and creates let mut queue = VecDeque::new() for the FIFO queue.
  • Enqueue uses queue.push_back(value) for owned i32 values from the fixed array literal [10, 20, 30].
  • Dequeue uses while let Some(value) = queue.pop_front(). pop_front returns Option<i32>; Some(value) moves the integer into the loop body, and None ends the loop without a separate empty check.
  • Removed values are collected in let mut removed = Vec::new() with removed.push(value).
  • The trace records queue=[], enqueue to [10, 20, 30], one front removal to removed=[10] with queue=[20, 30], then draining to removed=[10, 20, 30] and queue=[].
  • render(&removed) borrows the result slice, maps each integer through to_string(), joins with " -> ", and println!("{}", ...) prints 10 -> 20 -> 30.