Struct std::collections::vec_deque::VecDeque

Overview

A double-ended queue implemented with a growable ring buffer.

The "default" usage of this type as a queue is to use push_back to add to the queue, and pop_front to remove from the queue. extend and append push onto the back in this manner, and iterating over VecDeque goes front to back.

A VecDeque with a known list of items can be initialized from an array:

use std::collections::VecDeque;

let deq = VecDeque::from::<Vec>([-1, 0, 1]);

Methods

fn new() -> VecDeque

Creates an empty deque.

Examples

use std::collections::VecDeque;

let deque = VecDeque::new();
fn with_capacity(count: u64) -> VecDeque

Creates an empty deque with space for at least capacity elements.

Examples

use std::collections::VecDeque;

let deque = VecDeque::with_capacity(10);
assert!(deque.capacity() >= 10);
fn from<Vec>(vec: Vec) -> VecDeque

Construct a VecDeque from a [Vec].

This is a cheap conversion.

Examples

use std::collections::VecDeque;

let buf = VecDeque::from::<Vec>([1, 2, 3]);
fn extend(self, value: any)

Extend this VecDeque with something that implements the [INTO_ITER] protocol.

Examples

use std::collections::VecDeque;

let deque = VecDeque::new();
deque.extend([1, 2, 3]);

assert_eq!(Some(1), deque.pop_front());
assert_eq!(Some(3), deque.pop_back());
fn insert(self, index: u64, value: any)

Inserts an element at index within the deque, shifting all elements with indices greater than or equal to index towards the back.

Element at index 0 is the front of the queue.

Panics

Panics if index is greater than deque's length

Examples

use std::collections::VecDeque;

let buf = VecDeque::new();
buf.push_back('a');
buf.push_back('b');
buf.push_back('c');
assert_eq!(buf, ['a', 'b', 'c']);

buf.insert(1, 'd');
assert_eq!(buf, ['a', 'd', 'b', 'c']);
fn iter(self) -> Iter

Returns a front-to-back iterator.

Examples

use std::collections::VecDeque;

let buf = VecDeque::new();
buf.push_back(5);
buf.push_back(3);
buf.push_back(4);

assert_eq!([5, 3, 4], buf.iter());
assert_eq!([4, 3, 5], buf.iter().rev());
fn from_iter(it: any) -> VecDeque

Build a VecDeque from an iterator.

The vecdeque can be converted from anything that implements the [INTO_ITER] protocol.

Examples

use std::collections::VecDeque;

let deque = VecDeque::from_iter(["a", "b", "c"]);
assert_eq!(deque.len(), 3);
assert_eq!(deque.pop_front(), Some("a"));
assert_eq!(deque.pop_back(), Some("c"));
assert_eq!(deque.len(), 1);
fn reserve(self, index: u64)

Reserves capacity for at least additional more elements to be inserted in the given deque. The collection may reserve more space to speculatively avoid frequent reallocations.

Panics

Panics if the new capacity overflows usize.

Examples

use std::collections::VecDeque;

let buf = VecDeque::from::<Vec>([1]);
buf.reserve(10);
assert!(buf.capacity() >= 11);
fn len(self) -> u64

Returns the number of elements in the deque.

Examples

use std::collections::VecDeque;

let deque = VecDeque::new();
assert_eq!(deque.len(), 0);
deque.push_back(1);
assert_eq!(deque.len(), 1);
fn capacity(self) -> u64

Returns the number of elements the deque can hold without reallocating.

Examples

use std::collections::VecDeque;

let buf = VecDeque::with_capacity(10);
assert!(buf.capacity() >= 10);
fn front(self) -> Option

Provides a reference to the front element, or None if the deque is empty.

Examples

use std::collections::VecDeque;

let d = VecDeque::new();
assert_eq!(d.front(), None);

d.push_back(1);
d.push_back(2);
assert_eq!(d.front(), Some(1));
fn back(self) -> Option

Provides a reference to the back element, or None if the deque is empty.

Examples

use std::collections::VecDeque;

let d = VecDeque::new();
assert_eq!(d.back(), None);

d.push_back(1);
d.push_back(2);
assert_eq!(d.back(), Some(2));
fn push_back(self, value: any)

Appends an element to the back of the deque.

Examples

use std::collections::VecDeque;

let buf = VecDeque::new();
buf.push_back(1);
buf.push_back(3);
assert_eq!(Some(3), buf.back());
fn push_front(self, value: any)

Prepends an element to the deque.

Examples

use std::collections::VecDeque;

let d = VecDeque::new();
d.push_front(1);
d.push_front(2);
assert_eq!(d.front(), Some(2));

Removes the first element and returns it, or None if the deque is empty.

Examples

use std::collections::VecDeque;

let d = VecDeque::new();
d.push_back(1);
d.push_back(2);

assert_eq!(d.pop_front(), Some(1));
assert_eq!(d.pop_front(), Some(2));
assert_eq!(d.pop_front(), None);

Removes the last element from the deque and returns it, or None if it is empty.

Examples

use std::collections::VecDeque;

let buf = VecDeque::new();
assert_eq!(buf.pop_back(), None);
buf.push_back(1);
buf.push_back(3);
assert_eq!(buf.pop_back(), Some(3));
fn remove(self, index: u64) -> Option

Removes and returns the element at index from the deque. Whichever end is closer to the removal point will be moved to make room, and all the affected elements will be moved to new positions. Returns None if index is out of bounds.

Element at index 0 is the front of the queue.

Examples

use std::collections::VecDeque;

let buf = VecDeque::new();
buf.push_back(1);
buf.push_back(2);
buf.push_back(3);
assert_eq!(buf, [1, 2, 3]);

assert_eq!(buf.remove(1), Some(2));
assert_eq!(buf, [1, 3]);
fn rotate_left(self, mid: u64)

Rotates the double-ended queue mid places to the left.

Equivalently,

  • Rotates item mid into the first position.
  • Pops the first mid items and pushes them to the end.
  • Rotates len() - mid places to the right.

Panics

If mid is greater than len(). Note that mid == len() does not panic and is a no-op rotation.

Complexity

Takes *O*(min(mid, len() - mid)) time and no extra space.

Examples

use std::collections::VecDeque;

let buf = (0..10).iter().collect::<VecDeque>();

buf.rotate_left(3);
assert_eq!(buf, [3, 4, 5, 6, 7, 8, 9, 0, 1, 2]);

for i in 1..10 {
   assert_eq!(i * 3 % 10, buf[0]);
   buf.rotate_left(3);
}

assert_eq!(buf, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
fn rotate_right(self, mid: u64)

Rotates the double-ended queue k places to the right.

Equivalently,

  • Rotates the first item into position k.
  • Pops the last k items and pushes them to the front.
  • Rotates len() - k places to the left.

Panics

If k is greater than len(). Note that k == len() does not panic and is a no-op rotation.

Complexity

Takes *O*(min(k, len() - k)) time and no extra space.

Examples

use std::collections::VecDeque;

let buf = (0..10).iter().collect::<VecDeque>();

buf.rotate_right(3);
assert_eq!(buf, [7, 8, 9, 0, 1, 2, 3, 4, 5, 6]);

for i in 1..10 {
   assert_eq!(0, buf[i * 3 % 10]);
   buf.rotate_right(3);
}

assert_eq!(buf, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);

Trait Implementations

impl PartialEq for VecDeque
fn eq(value: any, value1: any) -> bool

Compare two values for equality.

Examples

assert_eq!(1.eq(2), false);
assert_eq!(2.eq(2), true);
assert_eq!(2.eq(1), false);
fn ne(value: any, value1: any) -> bool

Compare two values for inequality.

Examples

assert_eq!(1.ne(2), true);
assert_eq!(2.ne(2), false);
assert_eq!(2.ne(1), true);
impl Eq for VecDeque
impl PartialOrd for VecDeque
fn partial_cmp(value: any, value1: any) -> Option

Compare two values.

Examples

use std::cmp::Ordering;

assert_eq!(1.partial_cmp(2), Some(Ordering::Less));
assert_eq!(2.partial_cmp(2), Some(Ordering::Equal));
assert_eq!(2.partial_cmp(1), Some(Ordering::Greater));
fn lt(value: any, value1: any) -> bool

Tests less than (for self and other) and is used by the < operator.

Examples

assert_eq!(1.0 < 1.0, false);
assert_eq!(1.0 < 2.0, true);
assert_eq!(2.0 < 1.0, false);
fn le(value: any, value1: any) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator.

Examples

assert_eq!(1.0 <= 1.0, true);
assert_eq!(1.0 <= 2.0, true);
assert_eq!(2.0 <= 1.0, false);
fn gt(value: any, value1: any) -> bool

Tests greater than (for self and other) and is used by the > operator.

Examples

assert_eq!(1.0 > 1.0, false);
assert_eq!(1.0 > 2.0, false);
assert_eq!(2.0 > 1.0, true);
fn ge(value: any, value1: any) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator.

Examples

assert_eq!(1.0 >= 1.0, true);
assert_eq!(1.0 >= 2.0, false);
assert_eq!(2.0 >= 1.0, true);
impl Ord for VecDeque
fn cmp(value: any, value1: any) -> Ordering

Compare two values.

Examples

use std::cmp::Ordering;

assert_eq!(1.cmp(2), Ordering::Less);
assert_eq!(2.cmp(2), Ordering::Equal);
assert_eq!(2.cmp(1), Ordering::Greater);
fn min(value: any, value1: any) -> Ordering

Return the minimum of two values.

Examples

assert_eq!(1.min(2), 1);
assert_eq!(2.min(2), 2);
assert_eq!(2.min(1), 1);
fn max(value: any, value1: any) -> Ordering

Return the maximum of two values.

Examples

assert_eq!(1.max(2), 2);
assert_eq!(2.max(2), 2);
assert_eq!(2.max(1), 2);

Protocols

protocol INTO_ITER
for item in value { }

Allows the value to be converted into an iterator in a for-loop.

protocol INDEX_GET
let $out = value[index]

Allows an indexing get operation to work.

protocol INDEX_SET
value[index] = $input

Allows an indexing set operation to work.

protocol DEBUG_FMT
format!("{:?}", value)

Write a debug representation to a string.

This calls the [DEBUG_FMT] protocol over all elements of the collection.

Examples

use std::collections::VecDeque;

let deque = VecDeque::from::<Vec>([1, 2, 3]);
assert_eq!(format!("{:?}", deque), "[1, 2, 3]");
protocol PARTIAL_EQ
if value == b { }

Perform a partial equality check with this deque.

This can take any argument which can be converted into an iterator using [INTO_ITER].

Examples

use std::collections::VecDeque;

let deque = VecDeque::from::<Vec>([1, 2, 3]);

assert!(deque == [1, 2, 3]);
assert!(deque == (1..=3));
assert!(deque != [2, 3, 4]);
protocol EQ
if value == b { }

Perform a total equality check with this deque.

Examples

use std::collections::VecDeque;
use std::ops::eq;

let deque = VecDeque::from::<Vec>([1, 2, 3]);

assert!(eq(deque, VecDeque::from::<Vec>([1, 2, 3])));
assert!(!eq(deque, VecDeque::from::<Vec>([2, 3, 4])));
protocol PARTIAL_CMP
if value < b { }

Perform a partial comparison check with this deque.

Examples

use std::collections::VecDeque;

let deque = VecDeque::from::<Vec>([1, 2, 3]);

assert!(deque > VecDeque::from::<Vec>([0, 2, 3]));
assert!(deque < VecDeque::from::<Vec>([2, 2, 3]));
protocol CMP
if value < b { }

Perform a total comparison check with this deque.

Examples

use std::collections::VecDeque;
use std::cmp::Ordering;
use std::ops::cmp;

let deque = VecDeque::from::<Vec>([1, 2, 3]);

assert_eq!(cmp(deque, VecDeque::from::<Vec>([0, 2, 3])), Ordering::Greater);
assert_eq!(cmp(deque, VecDeque::from::<Vec>([2, 2, 3])), Ordering::Less);
protocol LT
if $a < $b { }

The protocol behind the < operator.

protocol LE
if $a <= $b { }

The protocol behind the <= operator.

protocol GT
if $a > $b { }

The protocol behind the > operator.

protocol GE
if $a >= $b { }

The protocol behind the >= operator.

protocol MIN
$a.min($b)

The implementation protocol for the PartialOrd::min method.

protocol MAX
$a.max($b)

The implementation protocol for the PartialOrd::max method.