Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That's true and I liked the idea of it until I started writing some Zig where I needed to work with strings. Very painful. I'm sure you typically get a bit faster string manipulation code than what you'd get with Rust but I don't think it's worth the cost (Rust is pretty fast already).


Having worked with c++ strings and also string views I find zigs simple fat pointer to be fairly direct and straightforward, and a bit refreshing. Ownership is being coupled in with the type..

Can't agree more. I hope someone puts some work into a less painful way to manage strings in std. I would but I don't manipulate strings quite enough to support usecases more than basically concatenation...


As of 0.15.X, you can build strings using a std.Io.Writer. You can either:

- use std.Io.Writer.fixed to use a slice for the memory, and use .buffered() when you're done to get the subslice of the buffer that contains your string

or

- Create an instance of std.Io.Writer.Allocating with an allocator, and use .toOwnedSlice() when you're done to get your allocated string.

In both cases you just use regular print functions to build your string.

Depending on your needs, it may also be good to use a fixed writer with a dynamically allocated slice, where the size of the allocation is computed using std.fmt.count(). This can be better than using std.Io.Writer.Allocating because you can avoid doing multiple allocations.


Delightful, I'll play about with both, thanks!



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: