←back to thread

Zig is hard but worth it

(ratfactor.com)
401 points signa11 | 3 comments | | HN request time: 0s | source
Show context
stared ◴[] No.36151295[source]
What are the use-cases in which it might be worth switching from Rust to Zig?
replies(2): >>36151409 #>>36151637 #
1. logicchains ◴[] No.36151409[source]
When you need to be very careful about memory allocation and use various custom allocators for stuff, and you don't care too much about memory safety. Rust makes working with custom allocators somewhat painful, in exchange for safety, so if you don't need the safety, no point going through that pain.
replies(1): >>36156714 #
2. notfed ◴[] No.36156714[source]
What kinds of programs don't care about memory safety?
replies(1): >>36195713 #
3. nyberg ◴[] No.36195713[source]
Compiler enforced memory safety*. Games in particular tend to have their own allocators and memory management schemes which don't play well with global allocators. Using memory pools, frame (arena) allocators, system specific memory management, and so on. Handling memory in a very systematic way (avoiding the "malloc everywhere" anti-pattern) where ownership is clearly tied to systems and handles (integers (maybe with metadata)) passed instead of pointers helps keep this all sane. That and you can still throw a GC/Ref counting at things which have non-linear lifetimes if needed but there's no need to do so globally.

One-shot programs also don't always need it as they're often fine with a less sophisticated scheme of arena allocation or reusing memory by resetting temporary buffers.

You also have the option to classify pointers if you absolutely must pass them with similar techniques as https://dinfuehr.github.io/blog/a-first-look-into-zgc.