5 ms·
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
by ArrayBoundCheck 4y ago
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 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
- pkolaczk 4y agoCan 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.
- masklinn 4y ago> 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.
- pkolaczk 4y agoIn 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.
- masklinn 4y ago> 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.
- ArrayBoundCheck 4y ago> 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
- ArrayBoundCheck 4y ago> 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