How do I create a VecDeque from a vector?
Alternatively, you can also use From<Vec<T>>
:
fn from(other: Vec<T>) -> VecDeque<T>
Turn a
Vec<T>
into aVecDeque<T>
.This avoids reallocating where possible, but the conditions for that are strict, and subject to change, and so shouldn't be relied upon unless the
Vec<T>
came fromFrom<VecDeque<T>>
and hasn't been reallocated.
Example:
let vector: Vec<i32> = vec![0, 1, 2];
let vec_queue: VecDeque<i32> = VecDeque::from(vector);
use std::collections::VecDeque;
use std::iter::FromIterator;
fn main() {
let ring = VecDeque::from_iter(&[1, 2, 3]);
println!("{:?}", ring);
}
It only implements From<Vec<T>>
and [...]
isn't a Vec
. Since it implements FromIterator
, you can use any kind of iterator as a source with from_iter
. &[1, 2, 3]
is because from_iter
takes an Iterator
.