Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to create a queue, then enqueue and dequeue elements in Rust

2 Answers

0 votes
use std::collections::VecDeque;

// Define a Queue struct
struct Queue<T> {
    data: VecDeque<T>, // Use VecDeque for efficient front and back operations
}

impl<T: std::fmt::Display> Queue<T> {
    // Create a new empty queue
    fn new() -> Self {
        Queue {
            data: VecDeque::new(),
        }
    }

    // Enqueue: Add an element to the back of the queue
    fn enqueue(&mut self, item: T) {
        self.data.push_back(item);
    }

    // Dequeue: Remove and return the element from the front of the queue
    fn dequeue(&mut self) -> Option<T> {
        self.data.pop_front()
    }

    // Check if the queue is empty
    fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    // Get the size of the queue
    fn size(&self) -> usize {
        self.data.len()
    }
    
    fn print(&self) {
        if self.is_empty() {
            println!("Queue is empty.");
        } else {
            print!("Queue contents: ");
            for item in &self.data {
                print!("{} ", item);
            }
            println!();
        }
    }
}

fn main() {
    // Create a new queue
    let mut queue = Queue::new();

    // Enqueue elements
    queue.enqueue(10);
    queue.enqueue(20);
    queue.enqueue(30);
    queue.enqueue(40);
    
    queue.print();

    println!("Queue size after enqueuing: {}", queue.size());

    // Dequeue elements
    if let Some(value) = queue.dequeue() {
        println!("Dequeued: {}", value);
    }

    if let Some(value) = queue.dequeue() {
        println!("Dequeued: {}", value);
    }
    
    queue.print();

    println!("Queue size after dequeuing: {}", queue.size());

    // Check if the queue is empty
    println!("Is the queue empty? {}", queue.is_empty());
}



/*
run:

Queue contents: 10 20 30 40 
Queue size after enqueuing: 4
Dequeued: 10
Dequeued: 20
Queue contents: 30 40 
Queue size after dequeuing: 2
Is the queue empty? false

*/
  
 

 



answered Aug 23, 2025 by avibootz
0 votes
use std::collections::VecDeque;

fn main() {
    // Create an empty queue using VecDeque
    let mut queue: VecDeque<i32> = VecDeque::new();

    // Enqueue elements (push elements to the back of the queue)
    queue.push_back(10);
    queue.push_back(20);
    queue.push_back(30);
    queue.push_back(40);

    println!("Queue after enqueueing: {:?}", queue);

    // Dequeue elements (pop elements from the front of the queue)
    if let Some(front) = queue.pop_front() {
        println!("Dequeued element: {}", front);
    } else {
        println!("Queue is empty, nothing to dequeue.");
    }

    println!("Queue after dequeueing: {:?}", queue);

    // Peek at the front element without removing it
    if let Some(front) = queue.front() {
        println!("Front element: {}", front);
    } else {
        println!("Queue is empty, no front element.");
    }

    // Peek at the back element without removing it
    if let Some(back) = queue.back() {
        println!("Back element: {}", back);
    } else {
        println!("Queue is empty, no back element.");
    }

    // Check if the queue is empty
    if queue.is_empty() {
        println!("The queue is empty.");
    } else {
        println!("The queue is not empty.");
    }

    // Get the length of the queue
    println!("Queue length: {}", queue.len());
}




/*
run:

Queue after enqueueing: [10, 20, 30, 40]
Dequeued element: 10
Queue after dequeueing: [20, 30, 40]
Front element: 20
Back element: 40
The queue is not empty.
Queue length: 3

*/

 



answered Aug 23, 2025 by avibootz
...