std/slice

Slice<T> is a lightweight, non-owning "view" into a contiguous sequence of elements. It is primarily used to provide a safe and convenient interface for working with fixed-size arrays.

Overview

Usage

import "std/slice.zc"
import "std/io.zc"

fn main() {
    let arr: int[5] = [10, 20, 30, 40, 50];
    
    // Explicit slice creation
    let s = Slice<int>::from_array(arr, 5);
    
    // Direct iteration over the slice
    for val in s {
        println(f"{val}");
    }
    
    // Directly iterating over the array (auto-imports std/slice.zc)
    for val in arr {
        println(f"{val}");
    }
}

Struct Definition

struct Slice<T> {
    data: T*;
    len: usize;
}

Methods

Construction

MethodSignatureDescription
from_arraySlice<T>::from_array(ptr: T*, len: usize) -> Slice<T>Creates a slice view over an array pointer.
newSlice<T>::new(data: T*, len: usize) -> Slice<T>Alias for from_array.

Iteration

MethodSignatureDescription
iteratoriterator(self) -> SliceIter<T>Returns an iterator for use in for-in loops.

Access & Query

MethodSignatureDescription
lengthlength(self) -> usizeReturns the number of elements in the slice.
is_emptyis_empty(self) -> boolReturns true if the slice contains no elements.
getget(self, idx: usize) -> Option<T>Returns the element at idx, or None if out of bounds.
atat(self, idx: usize) -> Option<T>Alias for get.

Notes