Struct Queue

Source
pub struct Queue<T: Clone> { /* private fields */ }
Expand description

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);

Implementations§

Source§

impl<T: Clone> Queue<T>

Source

pub fn new() -> Queue<T>

Create a new queue

§Returns

A new, empty Queue<T>

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

Trait Implementations§

Source§

impl<T: Debug + Clone> Debug for Queue<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Clone> Default for Queue<T>

Source§

fn default() -> Queue<T>

Default queue initializer

§Returns

A new, empty Queue<T>

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

impl<T: Clone> IsQueue<T> for Queue<T>

Source§

fn add(&mut self, val: T) -> Result<Option<T>, &str>

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);
Source§

fn remove(&mut self) -> Result<T, &str>

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);
Source§

fn peek(&self) -> Result<T, &str>

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));
Source§

fn size(&self) -> usize

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> Freeze for Queue<T>

§

impl<T> RefUnwindSafe for Queue<T>
where T: RefUnwindSafe,

§

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

§

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

§

impl<T> Unpin for Queue<T>
where T: Unpin,

§

impl<T> UnwindSafe for Queue<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.