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 theClone
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]
Trait Implementations
impl<T: Debug + Clone> Debug for Queue<T>
[src]
fn fmt(&self, __arg_0: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl<T: Clone> Default for Queue<T>
[src]
impl<T: Clone> IsQueue<T> for Queue<T>
[src]
fn add(&mut self, val: T) -> Result<Option<T>, &str>
[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);
fn remove(&mut self) -> Result<T, &str>
[src]
Removes an element from the queue and returns it
Returns
Ok(T)
: The oldest element in the queueError
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);
fn peek(&self) -> Result<T, &str>
[src]
Peek at the head of the queue
Returns
Ok(T)
: The next element scheduled for removal from the queueError
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));