pub struct Deque<T> { /* private fields */ }Expand description
A double-ended queue (deque) backed by VecDeque.
Provides O(1) push and pop from both ends. Iteration order is
front-to-back by default; use .iter().rev() or Deque::iter_back
for back-to-front traversal.
§Differences from crate::Queue
crate::Queue is FIFO — elements enter at the back and leave at the front only.
Deque relaxes that: you can push and pop from either end independently,
making it suitable for sliding-window algorithms, palindrome checks,
work-stealing schedulers, and undo/redo stacks.
§Examples
use graph_collections::Deque;
let mut deque: Deque<u32> = Deque::new();
deque.push_back(2);
deque.push_front(1);
deque.push_back(3);
// front → back: [1, 2, 3]
assert_eq!(deque.front(), Some(&1));
assert_eq!(deque.back(), Some(&3));
assert_eq!(deque.pop_front(), Some(1));
assert_eq!(deque.pop_back(), Some(3));
assert_eq!(deque.len(), 1);Implementations§
Source§impl<T> Deque<T>
impl<T> Deque<T>
Sourcepub fn new() -> Deque<T>
pub fn new() -> Deque<T>
Creates a new, empty deque.
§Examples
use graph_collections::Deque;
let deque: Deque<u32> = Deque::new();
assert!(deque.is_empty());Sourcepub fn with_capacity(capacity: usize) -> Deque<T>
pub fn with_capacity(capacity: usize) -> Deque<T>
Creates a new, empty deque with at least the given capacity
pre-allocated, avoiding reallocations for the first capacity pushes.
§Examples
use graph_collections::Deque;
let deque: Deque<u32> = Deque::with_capacity(32);
assert!(deque.is_empty());Source§impl<T> Deque<T>
impl<T> Deque<T>
Source§impl<T> Deque<T>
impl<T> Deque<T>
Sourcepub fn front(&self) -> Option<&T>
pub fn front(&self) -> Option<&T>
Returns a reference to the front element, or None if empty.
Does not remove the element.
§Examples
use graph_collections::Deque;
let mut deque: Deque<u32> = Deque::new();
assert_eq!(deque.front(), None);
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.front(), Some(&1));
assert_eq!(deque.len(), 2); // unchangedSourcepub fn front_mut(&mut self) -> Option<&mut T>
pub fn front_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the front element, or None if empty.
§Examples
use graph_collections::Deque;
let mut deque: Deque<u32> = Deque::new();
deque.push_back(1);
if let Some(v) = deque.front_mut() {
*v = 99;
}
assert_eq!(deque.front(), Some(&99));Sourcepub fn back(&self) -> Option<&T>
pub fn back(&self) -> Option<&T>
Returns a reference to the back element, or None if empty.
Does not remove the element.
§Examples
use graph_collections::Deque;
let mut deque: Deque<u32> = Deque::new();
assert_eq!(deque.back(), None);
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.back(), Some(&2));
assert_eq!(deque.len(), 2); // unchangedSourcepub fn back_mut(&mut self) -> Option<&mut T>
pub fn back_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the back element, or None if empty.
§Examples
use graph_collections::Deque;
let mut deque: Deque<u32> = Deque::new();
deque.push_back(1);
if let Some(v) = deque.back_mut() {
*v = 42;
}
assert_eq!(deque.back(), Some(&42));Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Returns a reference to the element at index (front = 0), or None
if index is out of bounds.
§Examples
use graph_collections::Deque;
let deque: Deque<u32> = (10..=14).collect();
assert_eq!(deque.get(0), Some(&10));
assert_eq!(deque.get(2), Some(&12));
assert_eq!(deque.get(9), None);Source§impl<T> Deque<T>
impl<T> Deque<T>
Sourcepub fn push_back(&mut self, element: T)
pub fn push_back(&mut self, element: T)
Appends element to the back of the deque.
§Examples
use graph_collections::Deque;
let mut deque: Deque<u32> = Deque::new();
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.back(), Some(&2));Sourcepub fn push_front(&mut self, element: T)
pub fn push_front(&mut self, element: T)
Prepends element to the front of the deque.
§Examples
use graph_collections::Deque;
let mut deque: Deque<u32> = Deque::new();
deque.push_front(2);
deque.push_front(1);
assert_eq!(deque.front(), Some(&1));Source§impl<T> Deque<T>
impl<T> Deque<T>
Sourcepub fn pop_front(&mut self) -> Option<T>
pub fn pop_front(&mut self) -> Option<T>
Removes and returns the front element, or None if empty.
§Examples
use graph_collections::Deque;
let mut deque: Deque<u32> = Deque::new();
assert_eq!(deque.pop_front(), None);
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.pop_front(), Some(1));
assert_eq!(deque.pop_front(), Some(2));
assert_eq!(deque.pop_front(), None);Sourcepub fn pop_back(&mut self) -> Option<T>
pub fn pop_back(&mut self) -> Option<T>
Removes and returns the back element, or None if empty.
§Examples
use graph_collections::Deque;
let mut deque: Deque<u32> = Deque::new();
assert_eq!(deque.pop_back(), None);
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.pop_back(), Some(2));
assert_eq!(deque.pop_back(), Some(1));
assert_eq!(deque.pop_back(), None);Source§impl<T> Deque<T>
impl<T> Deque<T>
Sourcepub fn rotate_left(&mut self, n: usize)
pub fn rotate_left(&mut self, n: usize)
Rotates the deque n steps to the left: moves the front n elements
to the back.
Equivalent to calling push_back(pop_front()) n times, but O(n) not O(n²).
§Panics
Panics if n > self.len().
§Examples
use graph_collections::Deque;
let mut deque: Deque<u32> = (1..=5).collect();
deque.rotate_left(2);
// was [1,2,3,4,5], now [3,4,5,1,2]
assert_eq!(deque.front(), Some(&3));
assert_eq!(deque.back(), Some(&2));Sourcepub fn rotate_right(&mut self, n: usize)
pub fn rotate_right(&mut self, n: usize)
Rotates the deque n steps to the right: moves the back n elements
to the front.
Equivalent to calling push_front(pop_back()) n times, but O(n) not O(n²).
§Panics
Panics if n > self.len().
§Examples
use graph_collections::Deque;
let mut deque: Deque<u32> = (1..=5).collect();
deque.rotate_right(2);
// was [1,2,3,4,5], now [4,5,1,2,3]
assert_eq!(deque.front(), Some(&4));
assert_eq!(deque.back(), Some(&3));Source§impl<T> Deque<T>
impl<T> Deque<T>
Sourcepub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
Returns a front-to-back iterator over references to the elements.
The deque is not modified.
§Examples
use graph_collections::Deque;
let deque: Deque<u32> = (1..=4).collect();
let values: Vec<u32> = deque.iter().copied().collect();
assert_eq!(values, [1, 2, 3, 4]);Sourcepub fn iter_back(&self) -> Rev<Iter<'_, T>>
pub fn iter_back(&self) -> Rev<Iter<'_, T>>
Returns a back-to-front iterator over references to the elements.
Convenience wrapper around self.iter().rev(). Because std::collections::vec_deque::Iter
implements DoubleEndedIterator, reversing is O(1) setup with O(1)
per element — no allocation.
§Examples
use graph_collections::Deque;
let deque: Deque<u32> = (1..=4).collect();
let values: Vec<u32> = deque.iter_back().copied().collect();
assert_eq!(values, [4, 3, 2, 1]);Trait Implementations§
Source§impl<T> Extend<T> for Deque<T>
impl<T> Extend<T> for Deque<T>
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
Pushes all elements produced by the iterator to the back of the deque.
§Examples
use graph_collections::Deque;
let mut deque: Deque<u32> = Deque::new();
deque.push_back(1);
deque.extend([2, 3, 4]);
assert_eq!(deque.len(), 4);
assert_eq!(deque.back(), Some(&4));Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<T> FromIterator<T> for Deque<T>
impl<T> FromIterator<T> for Deque<T>
Source§fn from_iter<I>(iter: I) -> Deque<T>where
I: IntoIterator<Item = T>,
fn from_iter<I>(iter: I) -> Deque<T>where
I: IntoIterator<Item = T>,
Builds a Deque from any iterator. Elements are pushed to the back
in iteration order — front of the iterator becomes front of the deque.
§Examples
use graph_collections::Deque;
let deque: Deque<u32> = (1..=4).collect();
assert_eq!(deque.front(), Some(&1));
assert_eq!(deque.back(), Some(&4));
assert_eq!(deque.len(), 4);Source§impl<'a, T> IntoIterator for &'a Deque<T>
Borrowing iterator: for x in &deque
impl<'a, T> IntoIterator for &'a Deque<T>
Borrowing iterator: for x in &deque
Source§fn into_iter(self) -> <&'a Deque<T> as IntoIterator>::IntoIter
fn into_iter(self) -> <&'a Deque<T> as IntoIterator>::IntoIter
Iterates over references to elements front-to-back without consuming the deque.
§Examples
use graph_collections::Deque;
let deque: Deque<u32> = (1..=3).collect();
let sum: u32 = (&deque).into_iter().sum();
assert_eq!(sum, 6);
assert_eq!(deque.len(), 3); // deque is still aliveSource§impl<T> IntoIterator for Deque<T>
Consuming iterator: for x in deque
impl<T> IntoIterator for Deque<T>
Consuming iterator: for x in deque