summaryrefslogtreecommitdiff
path: root/docs/std/slice.md
diff options
context:
space:
mode:
authorczjstmax <maxwasmailed@proton.me>2026-01-31 20:09:51 +0100
committerGitHub <noreply@github.com>2026-01-31 20:09:51 +0100
commit5f283b75488e89d2c4f261ae83e0424daec29554 (patch)
treeba1637d3885213095b312f81a477c33b1ebca6aa /docs/std/slice.md
parentd2e2617dec584884b92eb452f377b20c0bf8f321 (diff)
parent13af49ba93d653fb6306604889c4ef66e9018873 (diff)
Merge branch 'z-libs:main' into main
Diffstat (limited to 'docs/std/slice.md')
-rw-r--r--docs/std/slice.md15
1 files changed, 9 insertions, 6 deletions
diff --git a/docs/std/slice.md b/docs/std/slice.md
index b70c5fe..f029995 100644
--- a/docs/std/slice.md
+++ b/docs/std/slice.md
@@ -10,12 +10,12 @@ import "std/slice.zc"
fn main() {
let arr: int[5] = [1, 2, 3, 4, 5];
- // Direct iteration (Recommended)
+ // Direct iteration (auto-imports std/slice.zc)
for val in arr {
println "{val}";
}
- // Manual slice creation (for partial views or specific needs)
+ // Manual slice creation
let slice = Slice<int>::from_array((int*)(&arr), 5);
for val in slice {
println "{val}";
@@ -39,6 +39,7 @@ struct Slice<T> {
| Method | Signature | Description |
| :--- | :--- | :--- |
| **from_array** | `Slice<T>::from_array(arr: T*, len: usize) -> Slice<T>` | Creates a slice view over an array. |
+| **new** | `Slice<T>::new(data: T*, len: usize) -> Slice<T>` | Alias for `from_array` (backwards compat). |
### Iteration
@@ -62,10 +63,10 @@ struct Slice<T> {
### Iterating over fixed-size arrays
```zc
+// std/slice.zc is auto-imported when using for-in on arrays
let numbers: int[3] = [10, 20, 30];
-let slice = Slice<int>::from_array((int*)(&numbers), 3);
-for n in slice {
+for n in numbers {
println "Number: {n}";
}
```
@@ -73,6 +74,8 @@ for n in slice {
### Safe indexed access
```zc
+import "std/slice.zc"
+
let arr: int[3] = [1, 2, 3];
let slice = Slice<int>::from_array((int*)(&arr), 3);
@@ -84,7 +87,7 @@ if (!opt.is_none()) {
## Notes
-- `Slice<T>` does not own its data - it's just a view
+- `Slice<T>` does not own its data — it's just a view
- No memory management needed (no `free()` method)
-- Must specify the generic type explicitly: `Slice<int>`, `Slice<String>`, etc.
+- **Auto-import**: `std/slice.zc` is automatically imported when using `for val in arr` on a fixed-size array
- The array pointer cast `(T*)(&arr)` is required for fixed-size arrays