std/stack

March 13, 2026 ยท View on GitHub

The std/stack module provides a LIFO (Last-In, First-Out) stack data structure.

Usage

import "std/stack.zc"

fn main() {
    let s = Stack<int>::new();
    s.push(10);
    s.push(20);
    
    let top = s.pop(); // Some(20)
} // s is freed automatically here

Struct Definition

struct Stack<T> {
    // Internal implementation details
}

Methods

Construction

MethodSignatureDescription
newStack<T>::new() -> Stack<T>Creates a new, empty stack.
cloneclone(self) -> Stack<T>Creates a deep copy of the stack.

Modification

MethodSignatureDescription
pushpush(self, value: T)Pushes a value onto the top of the stack.
poppop(self) -> Option<T>Removes and returns the top element of the stack. Returns None if empty.
clearclear(self)Removes all elements from the stack.

Access & Query

MethodSignatureDescription
lengthlength(self) -> usizeReturns the number of elements in the stack.
is_emptyis_empty(self) -> boolReturns true if the stack contains no elements.

Memory Management

MethodSignatureDescription
freefree(self)Manually frees the stack memory.
Traitimpl Drop for StackAutomatically calls free() when the stack goes out of scope.