News for hackers. Stuff that matters.
Re:How (memory) safe is Zig? (2021) by 90s_dev on Tuesday May 13, 2025 @07:08PM (#43978824)
> But it does not nearly approach the level of systematic prevention of memory unsafety that rust achieves.

Unless I gravely misunderstood Zig when I learned it, the Zig approach to memory safety is to just write a ton of tests fully exercising your functions and let the test allocators find and log all your bugs for you. Not my favorite approach, but your article doesn't seem to take into account this entirely different mechanism.

Re:How (memory) safe is Zig? (2021) by nine_k on Tuesday May 13, 2025 @07:26PM (#43979000)
I suppose you can even ship the test/logging allocator with your production build, and instruct your users to run your program with some option / env var set to activate it. This would allow to repro a problem right where it happens, hopefully with some info helpful for debugging attached.

Not a great approach for critical software, but may be much better than what C++ normally offers for e.g. game software, where the development speed definitely trumps correctness.

Re:How (memory) safe is Zig? (2021) by KerrAvon on Tuesday May 13, 2025 @07:53PM (#43979191)
What that means, though, is that you have a choice between defining memory unsafely away completely with Rust or Swift, or trying to catch memory problems by a writing a bunch of additional code in Zig.
Re:How (memory) safe is Zig? (2021) by TimSchumann on Tuesday May 13, 2025 @08:09PM (#43979307)
I’d argue that ‘a bunch of additional code’ to solve for memory safety is exactly what you’re doing in the ‘defining memory safety away’ example with Rust or Swift.

It’s just code you didn’t write and thus likely don’t understand as well.

This can potentially lead to performance and/or control flow issues that get incredibly difficult to debug.

Re:How (memory) safe is Zig? (2021) by agarren on Tuesday May 13, 2025 @09:34PM (#43979798)
That sounds a bit unfair. All that code that we neither wrote nor understood, I think in the case of Rust, it’s either the borrow checker or the compiler itself doing something it does best - i.e., “defining memory safety away”. If that’s the case, then labeling such tooling and language-enforced memory safety mechanisms as “a bunch of additional code…you didn’t write and…don’t understand” appears somewhat inaccurate, no?
Re:How (memory) safe is Zig? (2021) by rk06 on Tuesday May 13, 2025 @10:54PM (#43980256)
It is quite fair as far as rust is concerned. For simple data structures, like doubly linked list,are hard problems for rust
Re:How (memory) safe is Zig? (2021) by josephg on Tuesday May 13, 2025 @11:42PM (#43980553)
So? That wasn't the claim. The GP poster said this:

> This can potentially lead to performance and/or control flow issues that get incredibly difficult to debug.

Writing a linked list in rust isn't difficult because of control flow issues, or because rust makes code harder to debug. (If you've spent any time in rust, you quickly learn that the opposite is true.) Linked lists are simply a bad match up for the constraints rust's borrow checker puts on your code.

In the same way, writing an OS kernel or a high performance b-tree is a hard problem for javascript. So what? Every language has things its bad at. Design your program differently or use a different language.

Re:How (memory) safe is Zig? (2021) by josephg on Tuesday May 13, 2025 @11:32PM (#43980478)
> This can potentially lead to performance and/or control flow issues that get incredibly difficult to debug.

The borrow checker only runs at compile-time. It doesn't change the semantic meaning - or the resulting performance - of your code.

The borrow checker makes rust a much more difficult and frustrating language to learn. The compiler will refuse to compile your code entirely if you violate its rules. But there's nothing magical going on in the compiler that changes your program. A rust binary is almost identical to the equivalent C binary.

Re:How (memory) safe is Zig? (2021) by ajross on Tuesday May 13, 2025 @08:19PM (#43979383)
Weird that Swift is your totem for "managed/collected runtime" and not Java (or C#/.NET, or Go, or even Javascript). I mean, it fits the bill, but it's hardly the best didactic choice.
Re:How (memory) safe is Zig? (2021) by saagarjha on Wednesday May 14, 2025 @04:32AM (#43982240)
I don't think they said anything about that?
Re:How (memory) safe is Zig? (2021) by ajross on Wednesday May 14, 2025 @11:30AM (#43985756)
The point was that basically no one knows Swift, and everyone knows Java. If you want to point out a memory safe language in the "managed garbage-collected runtime" family, you probably shouldn't pick Swift.
Re:How (memory) safe is Zig? (2021) by orobio on Wednesday May 14, 2025 @04:03PM (#43988616)
I wouldn’t put Swift in the same ‘managed garbage-collected runtime’ family as Java, C#/.NET, Go, and Javascript, so maybe they weren’t trying to do what you think.

Swift is more like a native systems programming language that makes it easy to trade performance for ergonomics (and does so by default).

Re:How (memory) safe is Zig? (2021) by 90s_dev on Tuesday May 13, 2025 @08:37PM (#43979479)
What if -- stay with me now -- what if we solved it by just writing vastly less code, and having actually reusable code, instead of reinventing every type of wheel in every project? Maybe that's the real secret to sound code. Actual code reuse. I know it's a pipedream, but a man can dream, can't he?
Re:How (memory) safe is Zig? (2021) by codr7 on Tuesday May 13, 2025 @09:31PM (#43979783)
The way we've done code reuse up to this point rarely lives up to its promises.

I don't know what the solution is, but these days I'm a lot more likely to simply copy code over to a new project rather than try to build general purpose libraries.

I feel like that's part of the mess Rust/Swift are getting themselves tangled up in, everything depends on everything which turns evolution into more and more of an uphill struggle.

Re:How (memory) safe is Zig? (2021) by josephg on Tuesday May 13, 2025 @11:44PM (#43980565)
Why? In C I'd understand. But cargo and the swift package manager work great.

By all means, rewrite little libraries instead of pulling in big ones. But if you're literally copy+pasting code between projects, it doesn't take much work to pull that code out into a shared library.

Re:How (memory) safe is Zig? (2021) by 0x6c6f6c on Tuesday May 13, 2025 @10:47PM (#43980211)
Yeah that is the opposite take of recent posts that the Cargo/npm package dependence is way too heavy.

Saying we should rely on reusable modules is great and all, but that reusable code is going to be maintained by who now?

There's no sustainable pattern for this yet, most things are good graces of businesses or free time development, many become unmaintained over time- people who actually want to survive on developing and supporting reusable modules alone might actually be more rare than the unicorn devs.

Re:How (memory) safe is Zig? (2021) by 90s_dev on Tuesday May 13, 2025 @11:19PM (#43980402)
I meant in programming in general, not specific to Rust or Cargo.
Re:How (memory) safe is Zig? (2021) by saagarjha on Wednesday May 14, 2025 @04:33AM (#43982247)
No, this doesn't solve the problem. Libraries have security issues like every other codebase.
Re:How (memory) safe is Zig? (2021) by josephg on Tuesday May 13, 2025 @11:24PM (#43980434)
Yes, testing is Zig's answer. But that quote is right. Testing doesn't achieve the same kind of systematic prevention of memory bugs that rust does. (Or GC based languages like Go, Java, JS, etc.).

You can write tests to find bugs in any language. C + Valgrind will do most of the same thing for C that the debug allocator will do for zig. But that doesn't stop the avalanche of memory safety bugs in production C code.

I used to write a lot of javascript. At the time I swore by testing. "You need testing anyway - why not use it to find other kinds of bugs too?". Eventually I started writing more and more typescript, and now I can't go back. One day I ported a library I wrote for JSON based operational transform from javascript to typescript. That library has an insane 3:1 test:code ratio or something, and deterministic constraint testing. Despite all of that testing, the typescript type checker still found a previously unknown bug in my codebase.

As the saying goes, tests can only prove the presence of bugs. They cannot prove your code does not have bugs. For that, you need other approaches - like rust's borrow checker or a runtime GC.

Re:How (memory) safe is Zig? (2021) by throwawaymaths on Wednesday May 14, 2025 @01:38AM (#43981171)
static code analysis tools can also do it. there's no reason why the borrow checker must be in the compiler proper.
Re:How (memory) safe is Zig? (2021) by alpaca128 on Wednesday May 14, 2025 @04:04AM (#43982098)
There's also no reason to have a separate borrow checker if it could just be integrated in the compiler.

When a compiler has a borrow checker that means the language was already designed to enable borrow checking in the first place. And if a language can let you do borrow checking why would you use a separate tool?

Re:How (memory) safe is Zig? (2021) by josephg on Wednesday May 14, 2025 @05:13AM (#43982501)
Also it’s a great way to make sure every library in the ecosystem passes the borrow checker.
Re:How (memory) safe is Zig? (2021) by throwawaymaths on Wednesday May 14, 2025 @08:47AM (#43983880)
why do you expect compile time static analysis to fail at this? unless youre loading a precompiled asset?
Re:How (memory) safe is Zig? (2021) by josephg on Wednesday May 14, 2025 @09:11AM (#43984127)
I don't. I think compile time static analysis is great. Upthread you said this:

> there's no reason why the borrow checker must be in the compiler proper.

On a technical front, I completely agree. But there's an ecosystem benefit to having the borrow checker as part of the compiler. If the borrow checker wasn't in the compiler proper, lots of people would "accidentally forget" to run it. As a result, lots of libraries would end up on crates.io which fail the borrow checker's checks. And that would be a knock on disaster for the ecosystem.

But yes, there's nothing stopping you writing a rust compiler without a borrow checker. It wouldn't change the resulting binaries at all.

Re:How (memory) safe is Zig? (2021) by throwawaymaths on Wednesday May 14, 2025 @09:46AM (#43984484)
> lots of people would "accidentally forget" to run it

yeah, like how the sel4 guys accidentally forget to run their static analysis all the time.

You put a badge on CI. If you "forget to run" the static analysis, then people get on you for not running it. Or people get on you if you don't have the badge. Just like how people get on people for not writing programs in rust.

Re:How (memory) safe is Zig? (2021) by josephg on Wednesday May 14, 2025 @04:46PM (#43989022)
Q: If we can get men on the moon, why is my lawnmower such a piece of junk?

A: The engineers who got us in to space didn’t design your lawnmower.

The SeL4 team won’t forget to run their static analysis checks. But most people aren’t at their level. Most people just want to get on with it. The borrow checker is a pain in the neck to learn - and if it were optional, you better believe lots of people would avoid it forever. I may well have been in that camp too - I found it really hard to get my head around it!

If you want a modern C-like language without rust’s borrow checker, Zig or Odin is probably a better bet. They’re both fine languages.

> Just like how people get on people for not writing programs in rust.

Who? Where?

Re:How (memory) safe is Zig? (2021) by throwawaymaths on Wednesday May 14, 2025 @11:28PM (#43991558)
your argument doesn't hold water, because you are appealing to the existence of a schroedinger's programmer that simultaneously would use rust but also wouldn't turn on a borrow checker if it existed in another language.

My point is the fact that people use Rust means that people do care. And those people will run static analysis checks (as long those static analysis checks aren't the shitty or overly difficult to run), and they will notice that your library isn't checked. Or, better yet, they will integrate it into their project, run the checker on their project, find the problem in the lazy person's dependency and PR a fix or fork it if the maintainer is an asshole.

Here's the thing. Rust is currently "the only game in town" for spatiotemporal memory safety and that is sucking the air out of the atmosphere, because no one wants to invest time into trying other ideas. And that's a shame, because everyone makes assumptions that Rust must be the only way to do spatiotemporal safety (cough cough Ada/Spark). And it gives cover for people to claim things that are presumptuous and totally untested like our schroedinger's programmer here.

Rust has a lot lot lot of shitty things in it, like RAII, and proc macros, and pushing you into some incredibly complicated (and privileged) templated types that are doing some things under the hood (remember when rust switched from jemalloc to malloc?), and Rust hasn't ever gotten to keyword generics yet. Those of us who don't want that kind of BS in our language have to constantly be accused of not caring about memory safety which is hardly the truth.

Re:How (memory) safe is Zig? (2021) by josephg on Thursday May 15, 2025 @12:54AM (#43991963)
> your argument doesn't hold water, because you are appealing to the existence of a schroedinger's programmer that simultaneously would use rust but also wouldn't turn on a borrow checker if it existed in another language.

I'm appealing to the existence of people who would like rust very much, but find the borrow checker to be cumbersome / annoying / difficult. I suspect a lot of people would fall into this camp. Including a lot of people who admire Zig, Jai, Odin, dmd. And probably a lot of C++ developers.

In my opinion, most of the best parts of rust have nothing to do with the borrow checker. For example, sum types, the rust standard library, cargo & crates.io, traits, Result / Option, editions, rust's unit testing, etc.

If someone released a borrow checker for zig, would it get 100% adoption? No way. Especially if it was hard to learn, required adding unfamiliar "lifetime annotations" throughout your code and sometimes required you to refactor functions that you know are correct anyway.

> Here's the thing. Rust is currently "the only game in town" for spatiotemporal memory safety and that is sucking the air out of the atmosphere, because no one wants to invest time into trying other ideas. [...] Those of us who don't want that kind of BS in our language have to constantly be accused of not caring about memory safety which is hardly the truth.

I think we've found agreement. I've been saying this for years - Rust is humanity's first serious attempt at a programming language like this. Its like the first iPad. I think there's lots of ways a rust successor could learn from rust's mistakes and make a better language. I'm really looking forward to what a borrow checker might look like a few more languages down the line.

Re:How (memory) safe is Zig? (2021) by throwawaymaths on Wednesday May 14, 2025 @08:50AM (#43983917)
because it gets it out of the fast path compile cycle. do you need a borrow checker for `ls`? Probably not. don't use it. do you need it every time you work through intermediate ideas in a refactor? probably not. just turn it on in CI.
Re:How (memory) safe is Zig? (2021) by steveklabnik on Wednesday May 14, 2025 @02:34PM (#43987775)
The borrow checker is very fast.
Re:How (memory) safe is Zig? (2021) by throwawaymaths on Wednesday May 14, 2025 @11:18PM (#43991493)
computationally. But it slows down the programmer. It is not a zero-cost human operation. If it were, we wouldn't need computers to do it.
Re:How (memory) safe is Zig? (2021) by steveklabnik on Wednesday May 14, 2025 @11:29PM (#43991562)
I don’t think it slows me down, it speeds me up. It’s well known that surfacing issues earlier in a process saves time compared to later in a process.
Re:How (memory) safe is Zig? (2021) by alpaca128 on Wednesday May 14, 2025 @02:42PM (#43987871)
The borrow checker is not the slow part of the Rust compiler and lets me avoid bugs, why would I not always want to use it?

And if you put the borrow checker in the CI you massively increased the latency between writing the code and getting all relevant feedback from the compiler/tooling. This would do the opposite of what you intended.

Re:How (memory) safe is Zig? (2021) by throwawaymaths on Wednesday May 14, 2025 @11:23PM (#43991526)
You don't have to query all checks at all times. You only have to borrow check when you pr to main. What is the sum latency of the coder dealing with conceptual problem of satisfying the borrow checker at every stage during an arduous refactor where they don't fully have the destination model in mind (and might want to try things out only to discard them). Versus, just ignoring it, seeing how the chips fall, and fixing ownership errors in one or two passes at the end once you've settled om the code structure? Of course you don't have to take the full round trip-to-someone-else's-computer latency either. If you can run it in CI, I presume you could run it locally too, and borrow checking is not particularly slow for the computer.

You're also not (because one can't) quantifying the problem of a developer getting exasperated and saying, fuck it, it passes the borrow checker, good enough, instead of actually taking the time to make their code legible first and then making sure it is memory safe. This absolutely happens.

Re:How (memory) safe is Zig? (2021) by josephg on Wednesday May 14, 2025 @04:54PM (#43989102)
> do you need a borrow checker for `ls`? Probably not.

Does ls use references and objects with lifetimes? I bet it does. And if so, the answer is yes. You do need the borrow checker in rust to make sure it uses memory and lifetimes correctly.

If your program somehow doesn’t use references or owned objects, then the borrow checker doesn’t have any work to do. So there’s no harm done in leaving it on.

Re:How (memory) safe is Zig? (2021) by tialaramex on Wednesday May 14, 2025 @04:20AM (#43982183)
"But seatbelts would also work if everybody was just choosing to use them rather than us mandating their fitment and use, so I don't understand why facts are true"

Amusingly this is even true for the linter, nobody ran the C linter, more or less everybody runs the Rust linter, the resulting improvement in code quality is everything you'd hope. All humans love to believe they're above average, most are not and average is by definition a mediocre aspiration. Do better.

Re:How (memory) safe is Zig? (2021) by throwawaymaths on Wednesday May 14, 2025 @08:55AM (#43983965)
what the hell are you talking about. if you are writing security conscious software you should turn on a static checker and proudly show a badge that says "this code is memory safe". if youre writing a custom data pipeline to be used in a niche scientific field where the consumers are you and anyone that wants to repro your pipeline, and everything is in arenas, who the fuck cares. don't bother with static analysis.
Re:How (memory) safe is Zig? (2021) by josephg on Wednesday May 14, 2025 @09:21AM (#43984223)
If everything is in arenas, lifetimes get much easier.

But, the borrow checker doesn't just check lifetimes. It also checks ownership, and that variables either have a single mutable reference or immutable references. The optimizer assumes those invariants are maintained in the code. Many of its optimizations wouldn't be sound otherwise.

So, if you could compile code which fails the borrow checker, there's all sorts of weird and wonderful sources of UB eagerly waiting to give you a really bad day - from aliasing issues to thread safety problems to use-after-free bugs. The borrow checker has been around forever in rust. So I don't think anyone has any idea what the implications would be of compiling "bad" code.

Re:How (memory) safe is Zig? (2021) by throwawaymaths on Wednesday May 14, 2025 @09:42AM (#43984432)
Point being, there are many many individual programs where none of those things you talk about exist. So why not have a programming system where you can actually turn those things off for development velocity.

I'm rejecting the idea that "opt-in" is bad. Opt-out is of course better, but "no choice" is not good.

Re:How (memory) safe is Zig? (2021) by josephg on Wednesday May 14, 2025 @04:34PM (#43988870)
> there are many many individual programs where none of those things you talk about exist

Which things? Programs without mutable references and aliasing concerns? Can you give an example?

Having worked in rust for a few years now, I’m not convinced you’d gain much velocity by disabling the borrow checker. Some code would be a little simpler without lifetime annotations, but you’d also end up spending a lot more time debugging your code. The borrow checker and rust type system are insanely good at finding bugs at compile time. It just takes awhile of working in rust before you stop stubbing your toe on the borrow checker’s (sometimes silly) rules.

If you want easy to write rust, you can always just lean heavily on Box and Rc, and .clone() everywhere. The trade off is your code won’t be as performant - but that doesn’t matter much if you’re prototyping.

If you care that much, rust is opensource. Fork it and turn the borrow checker off when compiling. It’s probably not even that hard to do. I’d love to hear about the experience - and what it’s like using rust without the borrow checker.

Re:How (memory) safe is Zig? (2021) by saagarjha on Wednesday May 14, 2025 @04:30AM (#43982234)
Because you're always going to write some code that the tools can't reason about.
Re:How (memory) safe is Zig? (2021) by throwawaymaths on Wednesday May 14, 2025 @08:47AM (#43983886)
thats true for rust too (hence "unsafe")