#How do you create a 'string'?

1 messages · Page 1 of 1 (latest)

twin lantern
#
var value = try self.arena.alloc(u8, stuff.len);
@memcpy(value, stuff);

Zig complains that value is a local variable never mutated. So then I try this:

var value = try self.arena.alloc(u8, stuff.len);
@memcpy(&value, stuff);

But this too fails:

index.zig:47:25: error: type '*[]u8' is not an indexable pointer
              @memcpy(&value, stuff);

What is the correct way to create a longer lived 'string' from a buffer?

orchid badge
#

just make it const

#

memcpy is modifying the data that the slice is pointing to not the value of the slice itself

safe ocean
#

@twin lantern is this some advanced usage that is specific to your unique situation, or just the default that you found when learning the lang?

asking because ArrayList(u8) and []const u8 are much cleaner for dealing with the typical string usecases, so just in case you are not aware of them

twin lantern
#

The current rust version can run on an iPhone, but iOS likes to kill it regularly

hearty wing
hidden quiver
hearty wing
hidden quiver
#

just heap-allocate a slice then :)

hearty wing
hidden quiver
#

yep

hearty wing
#

Sounds good thanks 🙂

coarse coral
safe nest
hearty wing