# Standard Library: Slice (`std/slice.zc`) `Slice` is a lightweight, non-owning view into a contiguous sequence of elements. It's particularly useful for working with fixed-size arrays and enabling iteration. ## Usage ```zc import "std/slice.zc" fn main() { let arr: int[5] = [1, 2, 3, 4, 5]; // Direct iteration (Recommended) for val in arr { println "{val}"; } // Manual slice creation (for partial views or specific needs) let slice = Slice::from_array((int*)(&arr), 5); for val in slice { println "{val}"; } } ``` ## Structure ```zc struct Slice { data: T*; len: usize; } ``` ## Methods ### Construction | Method | Signature | Description | | :--- | :--- | :--- | | **from_array** | `Slice::from_array(arr: T*, len: usize) -> Slice` | Creates a slice view over an array. | ### Iteration | Method | Signature | Description | | :--- | :--- | :--- | | **iterator** | `iterator(self) -> SliceIter` | Returns an iterator for `for-in` loops. | `SliceIter` implements the iterator protocol with a `next() -> Option` method. ### Access & Query | Method | Signature | Description | | :--- | :--- | :--- | | **length** | `length(self) -> usize` | Returns the number of elements. | | **is_empty** | `is_empty(self) -> bool` | Returns true if length is 0. | | **get** | `get(self, idx: usize) -> Option` | Returns the element at index, or None if out of bounds. | | **at** | `at(self, idx: usize) -> Option` | Alias for `get`. | ## Examples ### Iterating over fixed-size arrays ```zc let numbers: int[3] = [10, 20, 30]; let slice = Slice::from_array((int*)(&numbers), 3); for n in slice { println "Number: {n}"; } ``` ### Safe indexed access ```zc let arr: int[3] = [1, 2, 3]; let slice = Slice::from_array((int*)(&arr), 3); let opt = slice.get(1); if (!opt.is_none()) { println "Value: {opt.unwrap()}"; // Prints: Value: 2 } ``` ## Notes - `Slice` does not own its data - it's just a view - No memory management needed (no `free()` method) - Must specify the generic type explicitly: `Slice`, `Slice`, etc. - The array pointer cast `(T*)(&arr)` is required for fixed-size arrays