Struct queues::Queue [] [src]

pub struct Queue<T: Clone> { /* fields omitted */ }

A simple FIFO queue with a growable size and no limit on its capacity.

Type parameters

  • T: Any type that implements the Clone trait.

Examples

This example uses the queue! macro to add elements to the queue. Note that the first element in the list of elements passed to the macro is considered the 'oldest'.

let mut q = queue![3isize, 4, 5];

// Add an element
assert_eq!(q.add(6), Ok(None));

// Remove some elements
assert_eq!(q.remove(), Ok(3));
assert_eq!(q.remove(), Ok(4));

// Peek at the next element scheduled for removal
assert_eq!(q.peek(), Ok(5));

// Check the queue size
assert_eq!(q.size(), 2);

Methods

impl<T: Clone> Queue<T>
[src]

[src]

Create a new queue

Returns

A new, empty Queue<T>

Examples

let q: Queue<isize> = Queue::new();
assert_eq!(q.size(), 0);

Trait Implementations

impl<T: Debug + Clone> Debug for Queue<T>
[src]

[src]

Formats the value using the given formatter. Read more

impl<T: Clone> Default for Queue<T>
[src]

[src]

Default queue initializer

Returns

A new, empty Queue<T>

Examples

let q: Queue<isize> = Queue::default();
assert_eq!(q.size(), 0);

impl<T: Clone> IsQueue<T> for Queue<T>
[src]

[src]

Adds an element to a queue

Parameters

  • val: Value to add to the queue

Returns

Ok(None) as the element addition should always be successful

Examples

let mut q: Queue<isize> = Queue::new();
assert_eq!(q.add(42), Ok(None));
assert_eq!(q.size(), 1);

[src]

Removes an element from the queue and returns it

Returns

  • Ok(T): The oldest element in the queue
  • Error

Errors

Returns an error if an attempt is made to remove an element from an empty queue

Examples

let mut q: Queue<isize> = Queue::new();
q.add(42);
assert_eq!(q.remove(), Ok(42));
assert_eq!(q.size(), 0);

[src]

Peek at the head of the queue

Returns

  • Ok(T): The next element scheduled for removal from the queue
  • Error

Errors

Returns an error if an attempt is made to peek into an empty queue

Examples

let mut q: Queue<isize> = Queue::new();
q.add(42);
assert_eq!(q.peek(), Ok(42));

[src]

Gets the size of the queue

Returns

The number of elements in the queue

Examples

let mut q: Queue<isize> = Queue::new();
assert_eq!(q.size(), 0);
let _ = q.add(42);
assert_eq!(q.size(), 1);

Auto Trait Implementations

impl<T> Send for Queue<T> where
    T: Send

impl<T> Sync for Queue<T> where
    T: Sync