docs / Getting Started / Queue & Stack

Queue & Stack

Queue<T> and Stack<T> add the two classic ordering disciplines on top of the collections above. Neither is a new primitive: both are built by composition over an existing collection, and both implement Iterable<T>, Queryable<T> and Foldable<T>, so that whole surface — for..in, contains, index_of, first, last, is_empty, reduce, any, all, count_where, sum, min, max — comes for free from the traits rather than being reimplemented on each type.

use system.io
use system.collections.queue
use system.collections.stack

fn main()
    // Queue<T> — first in, first out.
    let jobs = Queue<String>()
    jobs.enqueue("build")
    jobs.enqueue("test")
    jobs.enqueue("deploy")

    println(f"{jobs.length()} queued")
    match jobs.peek()
        Some(next): println(f"next: {next}")
        None: println("nothing queued")

    // dequeue returns T? — draining an empty queue yields None, never a trap.
    while jobs.length() > 0
        match jobs.dequeue()
            Some(job): println(f"running {job}")
            None: println("empty")

    // Stack<T> — last in, first out.
    let undo = Stack<String>()
    undo.push("type 'a'")
    undo.push("type 'b'")

    match undo.pop()
        Some(step): println(f"undid: {step}")
        None: println("nothing to undo")

    // Both are built by composition over the existing collections, so the
    // whole Iterable / Queryable / Foldable surface comes along for free.
    println(f"{undo.length()} step(s) left")
TypeOrderAddRemoveInspect
Queue<T>First in, first outenqueue(item T)dequeue() T?peek() T?
Stack<T>Last in, first outpush(item T)pop() T?peek() T?

Both also carry length() and element_at(index int). Note that removal and inspection return T?, not T: draining an empty queue yields None rather than trapping or handing back a sentinel value, so the empty case is one you handle in the type system like any other.

Neither implements Transformable, so map and filter are not available on a queue or a stack directly — iterate it, or build a List from it, when you need those.

Import them with use system.collections.queue and use system.collections.stack.