News for hackers. Stuff that matters.
Re:Some Thoughts on Zig by ArrayBoundCheck on Thursday July 21, 2022 @10:49PM (#32187905)
I never tried zig but I want to address something the article wrote

> People struggle with Rust for a number of reasons ... the borrow checker!

The borrow checker was the easiest thing for me to learn and the only thing I liked about rust. Except it's broken. I have to constantly reborrow after calling a constant function. The reason I actually bailed on rust is because it's a worse C++ (compiles slower, executes slower, isn't compatible with C's __thread etc etc), it's a worse webserver (I still use PHP and C#), it's worse for database heavy code, it's not very good for one offs (C# and python are better), it basically cost too much to switch for the broken borrow checker

Also the article at the end doesn't make it clear what he thinks. Usually I read the start and end to see if I'm interested in the topic and I'm not sure if this article is in secret a rant

Re:Some Thoughts on Zig by nemothekid on Thursday July 21, 2022 @11:00PM (#32187965)
>I have to constantly reborrow after calling a constant function.

Not sure what you mean by this, do you have an example?

Re:Some Thoughts on Zig by ArrayBoundCheck on Friday July 22, 2022 @12:09AM (#32188428)
Not sure? I wasn't aware people didn't get this problem. Maybe its the domain I'm in. Uncomment to fix. I have to repeat myself all the time and it's obnoxious (IDK how to show code on this site but heres a link https://play.rust-lang.org/?version=stable&mode=debug&editio...)
Re:Some Thoughts on Zig by nemothekid on Friday July 22, 2022 @03:09AM (#32189385)
Oop, I'm aware of this, it is very annoying, I think I just hit it just the other day; just had no idea what you were referring to from the description.
Re:Some Thoughts on Zig by ArrayBoundCheck on Friday July 22, 2022 @11:52AM (#32193319)
What do other people call it? > 50% of the time people don't know what I mean until I show code
Re:Some Thoughts on Zig by swsieber on Thursday July 21, 2022 @10:59PM (#32187959)
The borrow checker has been through a couple revisions now, and it's got better each time.
Re:Some Thoughts on Zig by ArrayBoundCheck on Thursday July 21, 2022 @11:58PM (#32188348)
I tried it again in 2021 and it still failed to deal with constant functions. Here's it again in 2022 https://play.rust-lang.org/?version=stable&mode=debug&editio...
Re:Some Thoughts on Zig by chlorion on Friday July 22, 2022 @01:02AM (#32188703)
There is nothing broken in this example, it's functioning exactly as it's intended to. What makes you think the borrow checker is broken here?

It appears like you don't really understand the rule about mutable and immutable references. Asking for the length of the vector requires an immutable reference, but you already have a mutable reference, which is explicitly not allowed by design (that's the whole point!).

So, you can only have one mutable reference to something if there are no immutable references, and multiple immutable references if there are no mutable references, you can never have both immutable and mutable references to something at the same time.

Re:Some Thoughts on Zig by nlitened on Friday July 22, 2022 @02:02AM (#32189011)
I don’t know Rust, just wondering: why is it forbidden to know a length of vector to which you hold a mutable reference? What could go wrong?
Re:Some Thoughts on Zig by masklinn on Friday July 22, 2022 @03:32AM (#32189493)
The problem is not “a length of a vector”, Rust is not that fine grained. As far as Rust is concerned, what can happen is that you acquire an immutable reference to an object while there’s a mutable (unique) reference outstanding, you can invalidate the immutable reference via the mutable one, and thus have a dangling reference, which is UB.

Hell, even here one could imagine two scoped threads (so they can work with references), one thread keeps modifying the vector, and on some ISAs the other might get torn values for the length, or possibly worse (I don’t know if worse is an option but I wouldn’t be surprised).

Re:Some Thoughts on Zig by suremarc on Friday July 22, 2022 @11:42AM (#32193187)
OP is just holding a reference to a single entry in the vector. You could write whatever you want to that entry, but it's not going to change the length of the vector. In some sense the problem is that Rust is not "fine grained" enough to figure out that OP's access pattern is valid.
Re:Some Thoughts on Zig by nickitolas on Saturday July 23, 2022 @02:58PM (#32206419)
It's not just about that. What rust sees is you are calling two functions: the len() function with a shared reference, and the indexing operator implementation for the Vec's [] (Which is just a function) with a mutable reference, which returns a mutable reference. The compiler does not look at the actual implementation of these 2 functions to figure things out, it only looks at the signatures (If it did look at the implementations, it would probably be slower, and what's worse, the caller's code could stop compiling if you change the implementation! That would be specially bad across a library boundary). The implementations are irrelevant AIUI to the borrow checker, only the signatures matter.
Re:Some Thoughts on Zig by scoutt on Friday July 22, 2022 @03:13AM (#32189418)
I imagine you can have one thread modifying the vector while trying to read its length from another thread.
Re:Some Thoughts on Zig by ArrayBoundCheck on Friday July 22, 2022 @05:31PM (#32197353)
Noone should imagine that. You definitely don't want two threads accessing the same vec or array at one time. Maybe if there's a lock
Re:Some Thoughts on Zig by nickitolas on Saturday July 23, 2022 @03:06PM (#32206510)
If it's read only, you can definitely want that, right? As long as the elements are Sync. And it should work (You might have a problem with the cleanup, but Arc or scoped threads should handle that). The problem is when one is a mutable reference, like in this case. AIUI you could have a single mutable reference to an element be used from another thread (For example, a scoped thread) if the elements of the Vec are Sync
Re:Some Thoughts on Zig by ArrayBoundCheck on Friday July 22, 2022 @11:51AM (#32193294)
I'm taking a reference to the array the vec is pointing to not the vec. Rust needs to be more fine grain because it's obnoxious and C++ container invalidation is less bad https://news.ycombinator.com/item?id=32188672
Re:Some Thoughts on Zig by fasterthanlime on Friday July 22, 2022 @06:54PM (#32198087)
This particular example is trivially restructured into a version that passes the current borrow checker: https://play.rust-lang.org/?version=stable&mode=debug&editio...

It isn't the gotcha you're looking for.

Re:Some Thoughts on Zig by magicalhippo on Friday July 22, 2022 @12:24AM (#32188510)
I don't know any rust but the error description[1] only talks about borrowing mutable from an immutable, which to me seems like an obvious error. However the compiler complains about the reverse condition. Again, I don't know any rust, so it's not clear why that's an error.

So is it a documentation bug or a compiler bug?

[1]: https://doc.rust-lang.org/stable/error-index.html#E0502

Re:Some Thoughts on Zig by ArrayBoundCheck on Friday July 22, 2022 @12:56AM (#32188672)
I only understand it because I learned how not to shoot myself in the foot with the canon that is C++

I'll sum up "container invalidation". In C or C++ if you append an array you may cause the array to grow which means it might have a different pointer. You basically can't take an address of an array you're planning to grow. All containers are like that except list I think which I never use

In rust, it's roughly the same idea. The problem is I borrowed something from vec (you can think of it as taking a reference or creating a pointer). I then call a function that is const (you can see here it's &self and not &mut self https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#1911). But that invalidates the borrow like push_back/resize/reserve and such in C++. Which is absolutely ridiculous because it's doesn't modify anything.

It makes me so mad because even the one thing I like about rust is annoying to use. Using an annoying checker, slower compiler, harder to optimize language is too much. I don't shoot myself in the foot in C++ so I'm unable to get a win in rust. For me using rust is more pain than practical

Re:Some Thoughts on Zig by pkolaczk on Friday July 22, 2022 @04:43AM (#32189909)
Can you share any compiler benchmarks showing Rust compiles slower than C++?

I can see something totally opposite in my projects - particularly recompiling after a change is very quick. And cargo check often runs in less than 1s. I never got such quick incremental recompile times in any of my earlier C++ projects, particularly when using templated or header heavy libraries like STL ;)

As for the borrow checker thing, it is not broken but it's simply a limitation. In a strictly statically checked it is usually possible to run into a situation of a valid program that gets rejected by the compiler. Here this limitation comes from the compiler following a pretty simple rule - only one reference is allowed if it is mutable. This simple rule is what makes many of Rust safety guarantees possible, and I guess relaxing it conditionally might be very tricky. NLLs have already made it a bit nicer recently, do maybe it is possible to improve it further.

Re:Some Thoughts on Zig by masklinn on Friday July 22, 2022 @07:10AM (#32190694)
> As for the borrow checker thing, it is not broken but it's simply a limitation.

Limitations are when the borrow checker is not smart enough to understand things like sub-scope borrows (fixed by NLL) or the interaction of mutable borrows and loops (which hopefully will eventually be fixed by Polonius aka NLLv2).

But here they want to acquire a shared reference to an object with an outstanding unique reference. That is quite literally the first of only two rules references have:

1. At any given time you can have either a single unique reference or multiple shared references.

2. References must be valid.

Re:Some Thoughts on Zig by pkolaczk on Friday July 22, 2022 @01:13PM (#32194477)
In the given example they are not using the shared reference at all during the whole lifetime of the immutable reference and the operation on the immutable one does not change the object at all, so it would be actually safe. This is the situation where those two rules are a bit too strict and reject a valid program.
Re:Some Thoughts on Zig by masklinn on Friday July 22, 2022 @01:54PM (#32195017)
> In the given example they are not using the shared reference at all during the whole lifetime of the immutable reference and the operation on the immutable one does not change the object at all, so it would be actually safe.

The compiler does not work at that level of resolution. It’s unclear that it should, even for a point query: there is no telling how much a weirdo ISA would bungle that if the two accesses came from different threads, for instance.

> This is the situation where those two rules are a bit too strict and reject a valid program.

Compilers will always, necessarily, reject valid programs. The alternative is to allow invalid programs.

Re:Some Thoughts on Zig by ArrayBoundCheck on Friday July 22, 2022 @02:51PM (#32195741)
> The compiler does not work at that level of resolution. It’s unclear that it should

The correct solution is to never ever use rust. It's a failed experiment that noone noticed failed

Re:Some Thoughts on Zig by ArrayBoundCheck on Friday July 22, 2022 @11:55AM (#32193371)
> Can you share any compiler benchmarks showing Rust compiles slower than C++?

You got to do this yourself because YMMV

My projects don't use many includes and little templates. So C++ is more than 4x faster. I think in one case it was 10x faster but other people might claim I'm cheating because most people don't write code that way. If you often write template heavy code or need to include many large headers you'll get different results