pub struct Queue<T: Clone> { /* fields omitted */ }
A simple FIFO queue with a growable size and no limit on its capacity.
T
: Any type that implements the Clone
trait.
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];
assert_eq!(q.add(6), Ok(None));
assert_eq!(q.remove(), Ok(3));
assert_eq!(q.remove(), Ok(4));
assert_eq!(q.peek(), Ok(5));
assert_eq!(q.size(), 2);
Create a new queue
A new, empty Queue<T>
let q: Queue<isize> = Queue::new();
assert_eq!(q.size(), 0);
Adds an element to a queue
val
: Value to add to the queue
Ok(None)
as the element addition should always be successful
let mut q: Queue<isize> = Queue::new();
assert_eq!(q.add(42), Ok(None));
assert_eq!(q.size(), 1);
Removes an element from the queue and returns it
Ok(T)
: The oldest element in the queue
Error
Returns an error if an attempt is made to remove an element from
an empty queue
let mut q: Queue<isize> = Queue::new();
q.add(42);
assert_eq!(q.remove(), Ok(42));
assert_eq!(q.size(), 0);
Peek at the head of the queue
Ok(T)
: The next element scheduled for removal from the queue
Error
Returns an error if an attempt is made to peek into an empty queue
let mut q: Queue<isize> = Queue::new();
q.add(42);
assert_eq!(q.peek(), Ok(42));
Gets the size of the queue
The number of elements in the queue
let mut q: Queue<isize> = Queue::new();
assert_eq!(q.size(), 0);
let _ = q.add(42);
assert_eq!(q.size(), 1);
Default queue initializer
A new, empty Queue<T>
let q: Queue<isize> = Queue::default();
assert_eq!(q.size(), 0);
Formats the value using the given formatter. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static