4 ms·
I don't think it's really that bad in Rust. If you're happy with an arena in Zig you can do exactly the same thing in Rust. There are a ton of options listed he
by IshKebab 10mo ago
I don't think it's really that bad in Rust. If you're happy with an arena in Zig you can do exactly the same thing in Rust. There are a ton of options listed here: https://donsz.nl/blog/arenas/ https://donsz.nl/blog/arenas/
Some of them even prevent use after free (the "ABA mitigation" column).
- mijoharas 10mo agoI'm not super experienced with zig, but I always think that in the same way that rust forces you to think about ownership (by having the borrow checker - note: I think of this as a good thing personally) zig makes you think upfront about your allocation (by making everything that can allocate take an allocator argument.). It makes everything very explicit, and you can always _see_ where your allocations are happening in a way that you can't (as easily, or as obviously - imo) in rust. It seems like something I quite like. I'm looking forward to rust getting an effects system/allocator api to help a little more with that side of things.
- silon42 10mo agoThe problem is deallocation... unless you tie the allocated object to an arena allocator with a lifetime somehow (Rust can model that).
- mijoharas 10mo agoYep, rust forces you to think about lifetimes. Zig only suggests it (because you're forced to think about allocation, which makes you naturally think about the lifetime usually) but does not help you with it/ensure correctness. It's still nice sometimes to ensure that you have to think about allocation everywhere, and can change the allocation strategy for something that works for your usecase. (hence why I'm looking forward to the allocator api in rust to get the best of both worlds).
- IshKebab 10mo agoThat'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).
- jamiejquinn 10mo agoCan'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...
- Zambyte 10mo agoAs 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.
- jamiejquinn 10mo agoDelightful, I'll play about with both, thanks!
- bnolsen 10mo agoHaving 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..
- pjmlp 10mo agoWhich is hardly any different from me using PurifyPlus back in 2000.
- IshKebab 10mo agoIt's very different.
- kryps 10mo agoNo, you can't do the same thing in Rust, because Rust crates and the standard library generally use the global allocator and not any arena you want to use in your code.
- IshKebab 10mo agoI mean you can store the nodes in an arena so you don't have to deal with the borrow checker getting upset with your non-tree ownership structure. That's the context. We weren't talking about arena use for speed/efficiency purposes. In that case you are right; it's much more awkward to use custom allocators in Rust.