Struct std::collections::vec_deque::VecDeque
OverviewA 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 VecDeque;
let deq = ;
Methods
Creates an empty deque with space for at least capacity
elements.
Examples
use VecDeque;
let deque = with_capacity;
assert!;
Extend this VecDeque with something that implements the [INTO_ITER
] protocol.
Examples
use VecDeque;
let deque = new;
deque.extend;
assert_eq!;
assert_eq!;
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 VecDeque;
let buf = new;
buf.push_back;
buf.push_back;
buf.push_back;
assert_eq!;
buf.insert;
assert_eq!;
Returns a front-to-back iterator.
Examples
use VecDeque;
let buf = new;
buf.push_back;
buf.push_back;
buf.push_back;
assert_eq!;
assert_eq!;
Build a VecDeque
from an iterator.
The vecdeque can be converted from anything that implements the [INTO_ITER
] protocol.
Examples
use VecDeque;
let deque = from_iter;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
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 VecDeque;
let buf = ;
buf.reserve;
assert!;
Returns the number of elements in the deque.
Examples
use VecDeque;
let deque = new;
assert_eq!;
deque.push_back;
assert_eq!;
Returns the number of elements the deque can hold without reallocating.
Examples
use VecDeque;
let buf = with_capacity;
assert!;
Provides a reference to the front element, or None
if the deque is empty.
Examples
use VecDeque;
let d = new;
assert_eq!;
d.push_back;
d.push_back;
assert_eq!;
Provides a reference to the back element, or None
if the deque is empty.
Examples
use VecDeque;
let d = new;
assert_eq!;
d.push_back;
d.push_back;
assert_eq!;
Appends an element to the back of the deque.
Examples
use VecDeque;
let buf = new;
buf.push_back;
buf.push_back;
assert_eq!;
Prepends an element to the deque.
Examples
use VecDeque;
let d = new;
d.push_front;
d.push_front;
assert_eq!;
Removes the first element and returns it, or None
if the deque is empty.
Examples
use VecDeque;
let d = new;
d.push_back;
d.push_back;
assert_eq!;
assert_eq!;
assert_eq!;
Removes the last element from the deque and returns it, or None
if it is empty.
Examples
use VecDeque;
let buf = new;
assert_eq!;
buf.push_back;
buf.push_back;
assert_eq!;
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 VecDeque;
let buf = new;
buf.push_back;
buf.push_back;
buf.push_back;
assert_eq!;
assert_eq!;
assert_eq!;
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 VecDeque;
let buf = .iter .;
buf.rotate_left;
assert_eq!;
for i in 1..10
assert_eq!;
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 VecDeque;
let buf = .iter .;
buf.rotate_right;
assert_eq!;
for i in 1..10
assert_eq!;
Trait Implementations
Compare two values for equality.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values for inequality.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values.
Examples
use Ordering;
assert_eq!;
assert_eq!;
assert_eq!;
Tests less than (for self
and other
) and is used by the <
operator.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Tests less than or equal to (for self
and other
) and is used by the <=
operator.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Tests greater than (for self
and other
) and is used by the >
operator.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Tests greater than or equal to (for self
and other
) and is used by the >=
operator.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values.
Examples
use Ordering;
assert_eq!;
assert_eq!;
assert_eq!;
Return the minimum of two values.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Return the maximum of two values.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Protocols
for item in value
Allows the value to be converted into an iterator in a for-loop.
format!
Write a debug representation to a string.
This calls the [DEBUG_FMT
] protocol over all elements of the collection.
Examples
use VecDeque;
let deque = ;
assert_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 VecDeque;
let deque = ;
assert!;
assert!;
assert!;
if value == b
Perform a total equality check with this deque.
Examples
use VecDeque;
use eq;
let deque = ;
assert!;
assert!;
if value < b
Perform a partial comparison check with this deque.
Examples
use VecDeque;
let deque = ;
assert!;
assert!;
if value < b
Perform a total comparison check with this deque.
Examples
use VecDeque;
use Ordering;
use cmp;
let deque = ;
assert_eq!;
assert_eq!;