7 ms·
Rust is not about memory safety
- perrygeo 2y agoAgree with this take. I think about Rust's memory safety about as much as I think about other language's garbage collectors. Which is to say occasionally, but certainly not every day. It's the underlying design that makes the language ergonomic, but not a daily consideration for average users. What I appreciate day-to-day about Rust is that it encourages (coerces you at compile time) to get things right. If you did not consider all possible code paths, you will hear about it immediately - not in a bug report 6 months from now. Where things are not right, they're obviously not so (ie unwrap). This gives me confidence that the rust code I ship will run in prod without failing due to some unforeseen error. Every error condition, aside from catastrophic hardware failure, has been accounted for in the AST of the program. That's not "proven correct" but it's close enough for production work.
- fargle 2y agoi don't disagree, but will register a bit of counter argument. i think it's great that the software engineering discipline is maturing. i think it's great that we study formal semantics and work towards making it easier to create correct programs and harder to create incorrect ones. new-thing-better, gotcha. but old-thing-bad-shouldn't-they-have-ism i do not like at all. i think it is a misrepresentation of what "C" was, and is, to falsely compare it with a modern "safe" language (we saw this before with java. oops i mean pascal. oops i mean ada, oops lisp, etc.). it was never meant to be a "safe" language. it was, and is, basically a portable high-level assembler. it allows, but does not enforce, "structured programming" and user defined types, and was portable. that was at the absolute forefront in the 1970s and 1980s. and the productivity was "off-the-chart". sure, your UNIX might crash now and then, but you "just yell down the hall and reboot it" instead of waiting on a multi-decade project to "do it right" to materialize. so what now? 50 years hence? is it an appropriate language for string parsing in a security sensitive application? probably not. is it a "bad" language, or inappropriate for some other task? and why aren't we wailing and screeching about the lack of memory safety in assembler, for example? the problem to me is not the language, but the application of it. say you have an application A where language X is a better fit than language Y, but they used X. you don't call out the designers of language X for not anticipating A, you should instead build A' using language Y and let it prove itself.
- thecodedmessage 2y agoThis is a fair defense of C, but C++ is what deserves the heat. C is portable assembly, C++ isn’t.
- belter 2y ago> Every error condition, aside from catastrophic hardware failure, has been accounted for in the AST of the program. That's not "proven correct" but it's close enough for production work. But not all possible program inputs...Different space size, but still relevant for some production scenarios.
- perrygeo 2y agoYes, but I can be assured that any input will follow one of the code paths that I explicitly wrote. There's no hidden runtime path that will bite me later, meaning that I had to at least reason about every non-happy path at some point in the dev process. It doesn't make the output "correct" but it's more likely to be correct than programming with happy-path-only optimism.
- satvikpendem 2y agoRust is just a popular ML like OCaml only it has a more sane package manager and a more imperative style than OCaml's recursive style.
- fire_lake 2y agoIt has a much more C-like syntax than MLs
- seanhunter 2y agoRust is not an ML. All languages in the ML family are garbage-collected. It also doesn't have ML syntax. Whereas I can take an SML program from my ML textbook and run it almost completely unaltered in ocaml or f# that is absolutely not the case for rust - it literally won't work at all.
- nequo 2y agoAnother way to look at Rust is that it’s a programming language in which the goal is to garbage collect at compile time. I don’t know if it’s the first one to do that. Maybe ATS fits this description too with its linear types, and ATS itself is considered an ML. With this view, Rust does not seem that far off from the various ML dialects.
- moonlion_eth 2y agoI've been saying this for awhile, glad I'm not alone
- gavinhoward 2y agoIn fact, I say Rust didn't go far enough. https://gavinhoward.com/2024/05/what-rust-got-wrong-on-formal-verification/ https://gavinhoward.com/2024/05/what-rust-got-wrong-on-forma...
- two_handfuls 2y agoOne thing I don’t understand there is why you say it’s ok for a function to return a value, but bad for it to return code (async or thread). What makes it bad? How does it make harder to prove properties of the program?
- gavinhoward 2y agoBecause it can make an assumption that the data will do nothing once it is returned. Sure, code is data (I've written Lisp before), but code isn't just Plain Old Data because it can do stuff.
- two_handfuls 2y agoWould a function pointer be equally bad in your book?
- gavinhoward 2y agoYes.
- two_handfuls 2y agoWhat property can’t we prove if we have function pointers? The article mentions trying to prove local properties of functions, and that’s all well and good, and similarly we could prove local properties of the function being pointed to. I guess what I’m saying is I think I’m missing something about why you are advocating removing those. It’s not just so we can statically prove that all destructors are called is it?
- Codrus 2y agoCompletely based take. Thank you for sharing!
- whateveracct 2y agorust is just too ugly for me - you can't even properly use its functions to build capital-A abstractions because it's compiler is too dumb to optimize them properly.
- jerven 2y agoCould you expand upon this? sounds like an interesting opinion/experience that is worth sharing in more detail.
- burjui 2y agoIt doesn't. Most people complaining about "ugliness" of Rust are either too lazy to read documentation (which is great btw), or just don't like the syntax, because it doesn't resemble their favorite language[s]. Some have valid criticism, but they usually lay it out straight, sometimes in the form of a blog post, instead of vague "Rust is bad" statements.
- whateveracct 2y agoi mean..don't act like it's impossible for someone to have a well-informed opinion that Rust is bad. It is an opinion and for some people, Rust is bad! I've given it multiple shakes and can't see myself using it. Don't like the values, the syntax, or the design. it's just a fact that i can't imagine a situation where i'd use rust over haskell. I'd sooner generate a C program via Haskell eDSL than use Rust if I were to do embedded work, for instance. And aside from embedded, Haskell clears Rust kind of comically if I lay out a matrix of what I care about in a PL.
- burjui 2y agoIt's not impossible. But if one says "Rust is bad", they better elaborate, unless their goal was to post a meaningless comment.
- whateveracct 2y ago
- mrkeen 2y ago> runtime exceptions are not the solution They are part of the solution. Good programming needs to handle errors. Monadic error-handling gets you most of the way, combining the local reasoning of return values with the terseness of unchecked exceptions. But there are simply too many places where something can go wrong to try to enforce deliberate error-handling everywhere. TFA mentioned indexing into arrays. Integer operations resulting in overflow is another example. To stay sane these fails just have to come out exceptionally. People writing about Java got it right for a long time: "Exceptions should be used exceptionally". But just like "null is the billion dollar mistake" it hasn't made it into the way people program yet.
- bennettnate5 2y ago> But there are too many places where something can go wrong to try and enforce deliberate error-handling everywhere Rust actually defines APIs to do just that. Integer overflows can be handled with the `checked_add` family of functions. Indexing can be handled with `get` and `get_mut` on slices. Some choose to develop Rust libraries/projects using only APIs that explicitly cannot panic. It's certainly not for everyone, but I find it incredible that Rust allows that option to develop code that is not only safe, but also robust against crashing the process (1). 1 - barring compiler/standard library bugs and OOM, which more so depends on the runtime
- Ingaz 2y ago"Exceptions should be used exceptionally" - I think golang `panic-recover` does exactly this. They made using `panic` intentionally hard. Motto: "Don't panic in your code"
- atoav 2y agoWhat Rust really shines in is in making you handle things that need to be handled. For example: your user might input valid UTF-8 text as a filename for saving. But not all filesystems allow all UTF-8 characters in filenames. Rust makes that explicit because it makes you convert the str/String to another special string type and forces you to handle errors in that conversion explicitly. Before I encountered that in Rust I have never even given it a thought, and I have been programming for years. And this is just one example, and also why I still recommend to anybody to learn Rust even if you will never use it. There are many valuable lessons in that language that will make you a better programmer in all other languages as well.
- 2OEH8eoCRo0 2y agoGive programmers enough rope and they'll always hang themselves.
- sdwr 2y agoA bit morbid, but very true. You have to be exceptionally disciplined to keep a codebase tidy.
- atoav 2y agoMy point was: It really depends on how you give them the rope. Is it a selection of ropes next to a cliff and 95% of the ropes will kill you without warning if you use it to go down it? Or it could be rope that prevents you from accidentally walking down the cliff, if you don't know it was there. Having a table saw that doesn't stop when it cuts wet material can be beneficial, but it is better to have one that stops when it hits your finger and where you can specifically deactivate the stopping-mechanism for the usecase of cutting slightly wet materials.
- dataflow 2y ago> you handle things that need to be handled. For example: your user might input valid UTF-8 text as a filename for saving. But not all filesystems allow all UTF-8 characters in filenames. Wait... isn't the usual problem that some filesystems allow invalid UTF sequences, rather than disallowing some valid ones? Or are you talking about ASCII characters like asterisks and colons? Also, don't all the idiosyncrasies differ per-filesystem, which... you won't even know until run time? How is converting to a different data type handling the things that need to be handled, when you have no idea what filesystem that data will be used for?
- el_pollo_diablo 2y agoDisclaimer: my shop develops non-trivial low-level software with machine-checked correctness proofs. Rice's theorem implies that automatic static analysis (like Rust's borrow checker) will only get you that far. To go further, you must equip the program with a proof, which can be an order of magnitude more complex than the program. Don't get me wrong, Rust is excellent for developing security-critical applications. Its borrow checker solves many memory safety and concurrency issues, and its type system more generally forces error paths to be explicit. The next step would be to use deductive methods to check that the error paths cannot be taken. > make invalid states unrepresentable That is a nice ideal, but not very practical. You need dependent types for that (for example, a sigma-type that bundles a value in a carrier type with a proof that it satisfies an invariant). They have their uses (e.g. at the interface of a library), but they are a pain to work with. You must build the proof terms together with the runtime values, thus entangling program and proofs into an unmaintainable mess. Oh, and forget about modular proof development: the dependent type encodes every constraint on the value, therefore most types must change across the program when you decide to prove an additional property of the program. You can't just keep the existing proof around and prove something else on top of it. By all means, please keep some invalid states representable, and cleanly separate the program from its various proofs.
- zozbot234 2y agoProof terms disappear when you compile the program, they have no representation at runtime. They amount to compile-time-checked capabilities at most. So there's no real "maintainance" issue with entangling programs and proofs.
- el_pollo_diablo 2y agoWhat does source code/proof maintenance have to do with what remains at runtime? Sure, you can always extract the computational part of a program by erasing the logical parts, but that does not imply that code intermixed with logical parts does not create maintenance issues. Dependency management is one of those issues. Proofs require an acyclic dependency graph, in order to prevent circular arguments. By intermixing code and proofs, you force everything to fit in a single global dependency order. The point is not that it is impossible (it is possible), but that it is a maintenance nightmare. Sometimes you have to break an otherwise perfectly coherent module into smaller modules, just because you need to insert some other modules between them in the dependency order, that are only relevant to some minor proof. You have to constantly refactor your code as you reason on more aspects of your program. Have you already tested and certified your code and existing proofs? Too bad, your latest proof will require lots of handwaving and recertification to convince everyone that the big refactoring does not threaten the existing properties. Runnable code stands on its own, and should not be polluted with logical parts for every aspect of the program one may want to reason about. You can establish any provable property of a program using only basic logical tools: a single precondition and a single postcondition per function, a single invariant per loop, maybe dependent types, a few ghost values here and there, etc, and stuff all your reasoning in them in huge messy conjunctions mixing all the concerns together. That would be the equivalent of using nothing but a Turing machine to write a program. It is possible, it is theoretically appealing, but it does not scale. Software engineering is a thing for a reason. So is proof engineering.
- CrisTal223 2y agoPlease reconsider your "a lower case only blog, purely for aesthetics." decision. It's a gimmick and makes it unnecessarily harder for readers.
- weatherlight 2y agongl, I didn't even notice until you pointed it out.
- Aerbil313 2y agoIt definitely screamed "I'm low-value surface-level content" to me even though the article was genuinely insightful.
- nsvd 2y agoI find it quite nice. Everyone has their own tastes I suppose.
- seivan 2y ago[dead]
- scotty79 2y agoFor me the most distinguishing trait of Rust is that it's value oriented (not handle oriented like pretty much all other popular languages). Once you start thinking about values (even for simple variables), the space they occupy (how much, Sized vs Sized?, stack, heap), moving them (which would invalidate borrow) it all starts to make sense. Assignment means "move" by default, not "copy" or "copy handle". Borrow checker is just a sidenote to that, created just to not have to move and clone stuff too much and expressive types are just nice but not unique. If you approach it as any other language and think in terms of handles you just get annoyed why your handles (pointers) are so clunky and restricted. Maybe it's just a bit like Git that its interface is easier to understand if you go from inside out. Interface follows what it's doing. It might be frustrating if you come from the opposite direction. From what you want it to do. But all other popular languages do that instead.
- mlsu 2y agoI mean, this is true of any low-level language like C++ or C. Manual memory management. I think it does drive a lot of people nuts who have only ever worked with GC languages. I think your reaction to Rust depends on where you approach it from: Abstract languages: "Ahhh! String, &str, Cow<str>!?" Low-level languages: "How come this isn't compiling? Oh, wow, I wouldn't have caught that. Cool!" I think at this time, there are a lot of people who are getting into Rust from the "top of the stack", so it is their first foray into manual memory management. Many critiques I have seen of Rust is that it is "too complex" for "no good reason." I think those seem superficially valid, especially if you compare Rust with C. "Just don't use after free bro, skill issue" is easy to say if you've only ever worked on a small, short-lived C codebase with one developer on it. Once you start working on a very large codebase with many contributors, people coming and going, "skill issue" is absolutely nonsensical. The borrow checker is about memory safety; but looking at it more broadly, at the sort of ecosystem-level view, it is about constraining individual programmers. Limiting what an individual programmer can do with the memory, and guiding them towards using memory safely/well, with good compiler warnings.
- amai 2y agohttps://en.wikipedia.org/wiki/Ada_(programming_language) https://en.wikipedia.org/wiki/Ada_(programming_language) has been focusing on correctness since 40 years.