Hacker News new | ask | show | jobs
by Zambyte 5 days ago
Conversely, Rust doesn't force you to explicitly handle if memory allocation failures. The least you can do in Zig is explicitly ignore allocation failures.
1 comments

You can certainly write data structures in Zig that swallow allocation failures rather than surfacing them. We're talking about library-level concerns, not language-level concerns. Both Rust and Zig give you the power to allocate raw memory and handle the result of that syscall however you want, it's the standard libraries that differ beyond that point.
Yes, it is a standard library difference. But unless you plan on rewriting the entire Rust ecosystem, you're going to be dealing with invisible failure points in Rust code that you would not be dealing with when writing the equivalent Zig code. The standard library is almost as fundamentally important to a language as it's built-in operators.

And Zig does have language-level features that combine to require you to explicitly handle propagated errors (error sets, requiring all return values be explicitly handled, requiring all switch cases be explicitly handled). Rust has a similar set of features (pattern matching, requiring all match cases be explicitly handled), but does not use them for allocation (Box::new, vec![], etc. does not return a Result value).

> But unless you plan on rewriting the entire Rust ecosystem, you're going to be dealing with invisible failure points in Rust code that you would not be dealing with when writing the equivalent Zig code.

That's what Rust's libcore is for. And the converse is that if your software is written for a system with overcommit--so every typical OS and distro these days--any error path having to do with memory allocation failure is impossible to trigger, because the OS won't honestly tell you if allocation would fail.

> And Zig does have language-level features that combine to require you to explicitly handle propagated errors

Of course, and Zig also doesn't stop you from writing a data structure that papers over allocation failures by using `std.process.exit` in the error path. Zig also discourages third-party dependencies more than Rust does, so I wouldn't be surprised if people are already wisely doing this when writing their code for programs targeting systems with overcommit.