Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

std.deque

from std.deque import Deque provides an owned generic ring buffer. It is ordinary embedded Plenty code with private list[Option[T]] storage; it does not change list indexing or list.pop(0) costs.

OperationResultContract
Deque[T]()Result[Deque[T], AllocError]Construct an empty deque
len(queue) or queue.len()i64Observe the live element count
queue.is_empty()boolObserve whether the count is zero
queue.append(value)Result[(), AllocError]Consume a value and append on the right
queue.appendleft(value)Result[(), AllocError]Consume a value and append on the left
queue.pop()Option[T]Remove and transfer the rightmost element
queue.popleft()Option[T]Remove and transfer the leftmost element
queue.clear()()Destroy elements from right to left, retaining capacity
from std.deque import Deque

def main() -> Result[(), AllocError | IoError]:
    mut queue = Deque[str]()?
    queue.append("b")?
    queue.append("c")?
    queue.appendleft("a")?
    print(len(queue), queue.pop())?
    while True:
        match queue.popleft():
            case Some(item):
                print(item)?
            case Nothing:
                break
    Ok(())
3 Option[str].Some("c")
a
b

Mutation requires a mutable owner or exclusive reference. Empty removals return Nothing. Stored elements follow ordinary collection storage restrictions, including affine owners; references, generators, and unit elements are unsupported.

End removal takes constant time apart from payload cleanup and allocates nothing. Appending is amortized constant time; growth takes linear time and doubles capacity, starting at eight slots. It reserves and initializes the full new buffer before transferring any existing element. Allocation or capacity failure preserves existing contents and drops the incoming value. Appending within capacity, removing, and clearing allocate no runtime storage.

The deque moves and requires explicit copy for independent duplication. It has no indexed access, membership operator, iteration protocol, bounded capacity, rotation, or bulk extension API. To consume it, repeatedly pop an end. See the runnable queue lesson.

Acknowledgements