Idiom for transforming ArrayList(u8) into []const u8

i’ve working with two different library functions:  one that produces an ArrayList(u8) as output, and one that consumes a []const u8 as input…

what’s the zig idiom that takes me from point A to point B???

1 Like

worked like a charm for me… :pray: :sunglasses:

in my case, i’m using zon2json to do what the name says; and then using std.json.parseFromSlice to do what its name says…

converting from List<T> to T[] was something i was continually doing in Java in the last millenium, using the former’s toArray method…

more recent “scripting” languages (with strings and garbage collection built-in) mask the need for this transformation in practice…

1 Like

I took another look at this. Just wanted to mention it, because in my case i ended up ditching toOwnedSlice and just using clearRetainingCapacity. It seemed to be a cleaner eay to keep all the ArrayList initialization logic outside of the main loop. I think it was also mentioned that toOwnedSlice can be good for dealing with something like sets where you need to grab different variables. So you would be iterating over something and calling it multiple times.

Note that this will make freeing the original .items slice illegal, since it is of a different length from the allocated slice, which is actually alist.items.ptr[0..alist.capacity], and free in Zig is sized.

1 Like