Is zig lang faster than rust?

In the podcast, he says the following thing about his untagged union trick:

And here's the kicker: This code is safe in Zig, because we have safety on untagged unions. We have both, it's safe. We didn't have to give up safety to accomplish this.

I have since then came across this comment that explains how this actually works:

In a safe build mode, bare unions have an extra, secret tag that the language uses to insert runtime safety checks when union fields are accessed. This causes the array of 8-byte bare unions to actually become an array of 16-byte bare unions due to 8-byte alignment. A heavy cost to pay for this runtime safety.

But we don't ship a safe build of the Zig compiler to our users. We ship a ReleaseFast build. In this build mode, bare unions do not have a secret safety tag, and they do not have runtime safety checks. Accessing the wrong field in this case is Undefined Behavior.

If you want to claim that your language does something better than Rust without giving up safety in the process, then I find it really disingenuous if it turns out that you achieved it by using a different definition of "safe" that allows for UB in optimized builds. You certainly gave up safety in comparison to Rust.

41 Likes