logoalt Hacker News

zozbot234today at 3:33 PM2 repliesview on HN

Even Rust has an unsafe subset. There's a meaningful question about how to make such an unsafe language as easy and comfortable to use as possible, and Zig is a decent answer to that particular question. The libraries Zig ships with are especially relevant here, rather than the core language per se: Rust's library facilities are outright terrible to write unsafe code with, because the usual requirements of safe code are assumed throughout and the whole point of unsafe code is to break those. (You can selectively relax those requirements, e.g. take &Cell<T> not &mut T, to allow for benign aliasing - but most stdlib code doesn't bother to!)


Replies

kibwentoday at 8:11 PM

Rust's library facilities are excellent for writing unsafe code with. Rust is very precise about which safety invariants must be upheld by which APIs, and does an excellent job documenting these, an excellent job at linting against improper usage of these APIs, and an excellent job leveraging the type system to reduce the need for unsafety in the first place. Writing unsafe Rust does indeed require its own degree of expertise compared to writing safe Rust, but fortunately it's trivial to just not write unsafe Rust; most Rust programmers only rarely have any cause to write unsafe code.

show 1 reply
oytistoday at 5:43 PM

"Like unsafe Rust, but more convenient to use" is basically every imperative language around there, I still don't understand the specific niche that Zig intended to fill

show 1 reply