#How to copy an item from a slice without assigning it to a new variable

1 messages · Page 1 of 1 (latest)

chrome stump
#

I have an arraylist of strings, and I want to modify parts of two different strings without changing the contents of the arraylist. I've tried

const final = alist.items[0][3..] + " " + alist.items[1][5..];

but that just throws src/main.zig:13:48: error: invalid operands to binary expression: 'Pointer' and 'Pointer'

subtle lake
#

Zig doesn't have runtime string concatenation. It doesn't really have a string type at all. To build a new string from parts, a fixed buffer or an arraylist are what you'd use.

chrome stump
#

Whats a fixed buffer?

#

Looks like a type of allocator, not sure if that's exactly what I need here? I'll try and hack around with an arraylist to work out what I need here

cyan gust
#

Also see std.mem.concat

#

Arraylist is a very general solution, and more easily refactorable, but concat should just work

#

But yeah, you don't get a free lunch when it comes to dynamic memory

chrome stump
#

That looks like exactly what I'll need

#

and the doc page has example usage!? I didn't think there were any examples of how to use zig in the docs... that's great

cyan gust
#

Sure, there'll surely be examples

chrome stump
#

I've not seen any so far

#

Just source code

cyan gust
#

But pro tip: reading the source code, and the tests in the source code, is the best source of examples

chrome stump
#

There doesn't appear to be an example for https://ziglang.org/documentation/master/std/#std.process.execve, not tests (if I click the "src" in the top, then CTRL+F for "execve", the only thing that comes up is the actual function, who's source code is already on that page, and doesn't really help me understand what it needs)

#

I'd argue showing the test on the documentation page is more useful than showing the source code

subtle lake
#

for a sys call like execve, I think most people look at the man page for the sys call. Mapping to Zig is usually pretty straightforward.

chrome stump
#

Fair enough, that makes sense

#

Wait, so this only returns an error. So, how do I get the output. I have to do something with the allocator?