Home
Rust
VecDeque Example
Updated Jan 5, 2025
Dot Net Perls
VecDeque. With the VecDeque in Rust, part of the std collections, we have a ring buffer. The front and back of the VecDeque can be added to or removed from with good performance.
Based on a Vector, the VecDeque helps us maintain the buffer's elements. To add elements, we use push_back or push_front. To remove elements, we use pop_back or pop_front.
Example. This program uses the Rust VecDeque in a variety of ways. It adds to the end and start with push_back and push_front, then loops over the elements.
Part 1 We create a VecDeque with the new() method. We call push_back 3 times and add integers to the buffer.
Part 2 We can use a for-loop to iterate over the elements stored within the VecDeque.
for
Part 3 Sometimes it can improve performance to use with_capacity to create a VecDeque. Here we also use push_front to add to the buffer's start.
Part 4 We can access the front and back with front() and back(). These return Options, so we can access them in a if-let statement.
Option
if
Part 5 Sometimes we may need to clone() the VecDeque. We can also convert it to a Vec with the collect method.
collect
Part 6 For removing elements, we can use pop_back and pop_front. We can even rotate elements left and right.
use std::collections::*; fn main() { // Part 1: create VecDeque and add 3 elements to it with push_back. let mut v = VecDeque::new(); v.push_back(100); v.push_back(200); v.push_back(300); // Part 2: loop over VecDeque. for element in v { println!("Element: {element}"); } // Part 3: create VecDeque with a starting capacity, and use push_front. let mut x = VecDeque::with_capacity(10); x.push_back("my"); x.push_back("friend"); x.push_back("!"); x.push_front("hello"); // Part 4: access front and back of VecDeque. if let Some(front) = x.front() { if let Some(back) = x.back() { println!("front = {}; back = {}", front, back); } } // Part 5: clone and convert into a Vec. let result = x.clone().into_iter().collect::<Vec<&str>>(); println!("clone {:?}", result); // Part 6: pop_back and rotate_left. x.pop_back(); println!("pop_back {:?}", x); x.rotate_left(1); println!("rotate_left {:?}", x); }
Element: 100 Element: 200 Element: 300 front = hello; back = ! clone ["hello", "my", "friend", "!"] pop_back ["hello", "my", "friend"] rotate_left ["my", "friend", "hello"]
Summary. As it uses a Vector internally, the VecDeque could be replaced with a Vector. But by using the pre-built VecDeque helper methods, our programs are shorter and easier to maintain.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jan 5, 2025 (new).
Home
Changes
© 2007-2025 Sam Allen