3 ms·
>When dealing in unsafe code — whether in Rust unsafe blocks or in all the code in languages like C and C++3 — the responsibility falls to the programmer to wri
by ActorNightly 3y ago
>When dealing in unsafe code — whether in Rust unsafe blocks or in all the code in languages like C and C++3 — the responsibility falls to the programmer to write code which is still safe. This is possible! It is simply very difficult.
This is an interesting Catch 22.
If you accept this premise as true, then Rust becomes ultimately pointless. Because the prescription would be to become a very good programmer and learn how to write memory safe code, at which point you really don't need any other language except C. You will likely write memorypools that have mechanisms to make sure that you never access memory you are not supposed to.
An alternative prescription is to dedicate effort to making higher level language compilers/transpilers better. That way everyone codes in Python, the compiler recognizes common access patterns, and does iterative optimization (probably ML powered) to get you the most optimal machine code.
If you accept the premise as false, i.e writing memory safe code is easy, then Rust doesn't really offer any benefits.
- constantcrying 3y ago>Because the prescription would be to become a very good programmer and learn how to write memory safe code, at which point you really don't need any other language except C. Yes. Indeed you don't need any other language than C. Every program could be written in it. The point of rust is to enforce strict rules on code that guarantees certain behaviour, but still allow operations which could violate those behaviours. >Because the prescription would be to become a very good programmer and learn how to write memory safe code No. The prescription is to be careful when dealing with memory. This is true for literally any language, the goal of rust is that the compiler can take away some of that workload.
- adgjlsfhk1 3y ago> Because the prescription would be to become a very good programmer and learn how to write memory safe code The problem isn't becoming a very good programmer. That's not difficult. The problem is becoming a very good programmer who is never tired or rushed. Any correct code in C will have implicit rules about how the memory is managed that are either written in comments or reside in the programmer's head. This doesn't work because eventually some will make a code change without understanding fully the implicit rule (often this person is the same person who figured out the rule).
- ActorNightly 3y ago>The problem is becoming a very good programmer who is never tired or rushed. So what stops that programmer from being lazy and wrapping all the blocks in unsafe in a Rust code base?
- adgjlsfhk1 3y agonothing, but a programmer who does so knows they are creating an abomination while in C, it's just a normal day. (and for anyone looking at the code, the rust version will look like a buggy mess, while the C version will look like correct C code).
- ActorNightly 3y agoYeah this doesn't play out in real world. Look at any large enterprise Java project. People take shortcuts and break things all the time, even with fairly strict typing and memory safety. Of course you can enforce things like "no unsafes ever in codebase" on a project, but if you have to reply on guidance for development, then you can simply say "first we write a complete set of tests that are a contract on correct operation, then we develop code", at which point the language choice becomes irrelevant.
- DrMeepster 3y agoBanning unsafe is actually really easy. Just one line `#![forbid(unsafe_code)]` at the start of your code.
- iknowstuff 3y agobottom line is: this doesn't happen. Feel free to study why not.
- __jem 3y agoWell, in the real world, the premise is false, but not because writing memory safe code is in fact easy, but because in aggregate in large code bases it's impossible.
- erik_seaberg 3y agoThe world would have looked a lot better if there had been a safe dialect of C that we could have used for 95% of the stack. Instead we wrote everything in the chainsaw juggling dialect.