std-method

VecDeque::as_slices

Borrow the logical sequence as up to two slices. Reading the first slice followed by the second preserves queue order. Exact split depends on storage state, so test the combined sequence rather than a fixed boundary. Expected stdout (not executed): [2, 3, 1] Common mistake: Assuming either slice has a particular length or that the first slice always contains the entire queue.

Formal Syntax & Structure

rust syntaxrust
pub fn as_slices(&self) -> (&[T], &[T])

Working Example

Example Snippetrust
fn main() {    use std::collections::VecDeque;    let mut q = VecDeque::from([1, 2, 3]);    let first = q.pop_front().unwrap();    q.push_back(first);    let (a, b) = q.as_slices();    let order: Vec<_> = a.iter().chain(b).copied().collect();    println!("{order:?}");}
Rust Reference