Struct std::collections::Deque
A double-ended queue (growable ring buffer).
Elements can be efficiently pushed to and popped from either end of the queue.
Example
use std::collections::Deque;
let deque: Deque<i32> = Deque::new();
defer deque.free();
deque.push(1);
deque.push(2);
deque.push_front(3);
deque.push_front(4);
assert_eq!(deque.len(), 4);
assert_eq!(deque.pop(), Option::some(2));
assert_eq!(deque.pop_front(), Option::some(4));
assert_eq!(deque.pop(), Option::some(1));
assert_eq!(deque.pop_front(), Option::some(3));
Run this example
Methods
impl Deque<T> { ... }
-
fn new() -> Deque<T>
Create an empty deque
This will not allocate until the first element is inserted.
-
Create a deque that can hold up to
capacity
elements without reallocating. -
fn from_slice(slice: &[T]) -> Deque<T>
Create a deque from a slice, copying the elements.
-
Create a deque from an iterator of elements.
-
Reserve capacity for at least
additional
elements. -
fn shrink_to_fit(self: &mut Deque<T>)
Shrink the size of the underlying buffer to the minimum size needed to hold the current elements.
-
Returns the number of elements in the deque.
-
Returns the total number of elements that the deque can hold without reallocating.
-
Returns
true
if the deque is empty,false
otherwise. -
fn clear(self: &mut Deque<T>)
Clears the deque, removing all values.
This does not shrink the underlying buffer, call shrink_to_fit to free up unused memory.
-
fn as_slices(self: &Deque<T>) -> (&[T], &[T])
Returns a pair of slices that represent the deque's contents in order.
If the deque has not wrapped around (elements are contiguous in memory), the second slice will be empty.
-
fn as_slices_mut(self: &mut Deque<T>) -> (&mut [T], &mut [T])
Returns a pair of mutable slices that represent the deque's contents in order.
If the deque has not wrapped around (elements are contiguous in memory), the second slice will be empty.
-
fn push(self: &mut Deque<T>, element: T)
Pushes an element to the back of the deque.
-
Pops an element from the back of the deque.
If the queue is empty, returns
Option::none()
. -
fn push_front(self: &mut Deque<T>, element: T)
Pushes an element to the front of the deque.
-
Pops an element from the front of the deque.
If the queue is empty, returns
Option::none()
. -
fn iter(self: &Deque<T>) -> DequeIterator<T>
-
fn iter_ref(self: &Deque<T>) -> DequeRefIterator<&T>
-
fn iter_mut(self: &mut Deque<T>) -> DequeRefIterator<&mut T>
Returns an iterator over mutable pointers to the elements of the collection.
See IterableMut for details.
-
Extends the deque on the back end with the contents of an iterator.
-
Extends the deque on the front end with the contents of an iterator.
-
fn extend_from_slice(self: &mut Deque<T>, value: &[T])
Extends the deque on the back end with the contents of a slice.
-
Removes the elements not mathing the given predicate.
Does not remove excess capacity, call shrink_to_fit afterwards, if this is desired.
Example
use std::collections::Deque; let queue: Deque<i32> = Deque::from_iter(&(0..=6)); defer queue.free(); queue.retain(|x: &i32| -> bool { *x % 2 == 0 }); assert_eq!(queue.len(), 4); let iter = queue.iter(); assert_eq!(iter.next(), Option::some(0)); assert_eq!(iter.next(), Option::some(2)); assert_eq!(iter.next(), Option::some(4)); assert_eq!(iter.next(), Option::some(6)); assert_eq!(iter.next(), Option::none());
Run this example -
fn free(self: &mut Deque<T>)
Frees the memory backing the object.
-
fn free_all(self: &mut Deque<T>)
Helper for collections that own heap-allocated objects. This is used to be able to do
defer col.free_all()
without much boilerplate.Example
use std::collections::Deque; use std::string::StringBuf; use std::fmt::format; let v: Deque<StringBuf> = Deque::new(); defer v.free_all(); for i in 1..10 { v.push("{} + {} = {}".format!(i, i, i + i).unwrap()); }
Run this example -
Returns a copy of the object.