VecDeque Make Contiguous Slices

Rotate a VecDeque ring buffer into one slice with make_contiguous, compare as_slices before and after, and keep pushing afterward.

RustData Integrity

Code

rustsnippet
28 lines
use std::collections::VecDeque;

fn main() {
    let mut queue: VecDeque<i32> = VecDeque::new();
    for value in 1..=5 {
        queue.push_back(value);
    }
    for _ in 0..3 {
        queue.pop_front();
    }
    for value in 6..=8 {
        queue.push_back(value);
    }

    // the ring buffer is split because the head advanced
    let (front, back) = queue.as_slices();
    println!("front {front:?} back {back:?}");
    println!("split: {}", !front.is_empty() && !back.is_empty());

    // make_contiguous rotates the ring into a single slice
    let slice = queue.make_contiguous();
    println!("contiguous: {slice:?}");
    println!("len: {}", queue.len());

    // further pushes keep working after the rotation
    queue.push_back(9);
    println!("after push: {:?}", queue.make_contiguous());
}

How the snippet works

The working surface is the function main — everything else is wiring.

Iteration happens inside for value in 1..=5 — everything outside it runs once.

Caveats

  • No package installation, no environment variables, no setup steps.

Using it

The demo output is labeled (front, split:, contiguous:), so every printed line maps back to its originating operation.

Key statements

let mut queue: VecDeque<i32> = VecDeque::new();
let (front, back) = queue.as_slices();
let slice = queue.make_contiguous();

Interactive Tools for This Topic

FAQ

What does VecDeque Make Contiguous Slices print when run?
Run it as a complete program — the output lines carry labels such as `front`, `split:`, `contiguous:`.