+4
-2
languages/ziglang/0.15/arraylist.md
+4
-2
languages/ziglang/0.15/arraylist.md
···
15
15
defer buf.deinit(alloc); // cleanup when we're done
16
16
17
17
try buf.print(alloc, "{s}: {d}", .{ name, value });
18
-
sendResponse(buf.items); // borrow the slice, arraylist still owns it
18
+
sendResponse(buf.items);
19
19
```
20
+
21
+
`buf.items` is the `[]u8` slice holding the actual data - a direct view into the arraylist's internal buffer. the arraylist still owns this memory, so don't hold onto it after `deinit`.
20
22
21
23
**build and return** - you're building something to give to a caller. they'll own the memory:
22
24
···
28
30
return buf.toOwnedSlice(alloc); // caller must free this
29
31
```
30
32
31
-
the key difference: `.items` gives you a view into the arraylist's memory (it still owns it). `.toOwnedSlice()` hands ownership to you (arraylist forgets about it, you must free it).
33
+
the key difference: `.items` borrows (arraylist still owns the memory, will free on `deinit`). `.toOwnedSlice()` transfers ownership (arraylist forgets about it, caller must free).
32
34
33
35
see: [dashboard.zig#L187](https://tangled.sh/@zzstoatzz.io/music-atmosphere-feed/tree/main/src/dashboard.zig#L187) for the return pattern
34
36