liveBook · Manning

Deque

As explained in Refactoring to Rust

A double-ended queue is a sequence data structure that supports insertion and removal at both its front and back.

A deque, or double-ended queue, supports insertion and removal at both its front and back. Rust provides it as VecDeque, backed by a growable Vec. section 3.2.3

Use VecDeque's operations according to the access pattern you need:

  • push_front and pop_front support stack behavior: the newest value is removed first. section 3.2.3
  • push_back and pop_back provide insertion and removal at the opposite end. section 3.2.3
  • A wrapper such as RpnStack can expose only the deque behavior required by an application. section 3.2.3

Using a deque as an RPN stack

Store parsed numbers with push_front, then retrieve operands with pop_front. The expression `3 4 +` pushes 3 and 4, pops them for addition, and pushes the result.

How Refactoring to Rust covers it

How deep it goes
Defines front and back operations, contrasts VecDeque with Vec, and demonstrates a stack wrapper without exploring internal storage mechanics.
Examples
Demonstrates RpnStack with arithmetic expressions including `3 4 +`, multiplication, division, and empty-stack failure.
What it assumes
Assumes basic Rust structs, methods, enums, Result handling, and mutable references.
What you can do after
Build it from scratch as a Rust wrapper that pushes and pops values at one deque end.
How it connects
Connects deques to stacks, Vec, growable arrays, reverse Polish notation, and error handling.

Also explained in

Compare how 3 books explain deque →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.