6 ms·
What part of rust prevents the writing of bugs? I understand it helps you avoid unsafe memory operations, but to say it gives you “bug free” code seems like an
by nkcmr 8y ago
What part of rust prevents the writing of bugs? I understand it helps you avoid unsafe memory operations, but to say it gives you “bug free” code seems like an falsehood.
- richardwhiuk 8y agoYou can mark values (e.g. Error conditions) as Must Use. This, along with structured enumerated types, means that you can't forget to handle error conditions. Other bugs such as the goto fail bug are eliminated by dead code spotting and consistent formatting. You can write bugs in Rust but they tend to be business logic bugs, or incorrect explicit decisions.
- the_mitsuhiko 8y agoDoesn’t “as good as any” just mean “not worse than any other language”?
- pasta1212 8y agoYes, but why would you say it in the negative form? Both forms mean it’s a tie or better.
- steveklabnik 8y agoRust can not guarantee correctness, but it is a focus in some sense. Strong static typing, robust error handling... you can encode a lot of invariants in the type system. Especially compared to something dynamically typed, like JavaScript. It’s not a proof assistant, but every bit helps!
- nkcmr 8y agoThanks for correcting me there. Rust _does_ have a more than the borrow checker that are helping the programmer avoid common dynamic language issues (and rust is awesome). I just know that even if the compiler of the language I am writing is very smart and can catch a lot of my silly mistakes, it would behoove me to think that I could write "bug free" code as a result. I would still write my code defensively, even in rust.
- steveklabnik 8y agoAbsolutely. I’ve written many bugs in Rust. But on the whole, my first attempts have less bugs overall. YMMV, of course.
- leshow 8y agoShould be noted it usually takes a bit longer to get to that first working version in Rust. It's debatable whether in the long run it's faster to develop with. I would argue it is much faster to get to a final, 'correct' working version because you spend less time debugging. Time is also drastically cut down the more familiar you are with the language, but still, I'd love to see some data on dev time between languages with robust type systems vs dynamic.
- KMag 8y agoDo you mean fewer bugs, or less severe bugs (or some other non-count metric other than severity)?
- chrismorgan 8y agoI expect my experience is much the same as Steve’s on this point. Mostly I would say fewer bugs; the bugs that are avoided vary in severity. Many, probably most, are the severe but obvious bugs where you would have hit the first time you tried running the code in another language, but which are instead now caught by the compiler—that’s why “time to first run” is not a reasonable metric for comparing writing code in such languages, but “time to first meaningfully successful run”! But it’s also common for Rust to catch and prevent far more insidious bugs; especially, in my experience, in two areas: firstly, with respect to ownership—things like passing data structures around and improperly mutating them where you should have cloned it instead, which is structurally impossible in Rust (disregarding things like Rc<RwLock<T>>, which make it possible, but make the intent very much clearer); and secondly, where enums (tagged unions) can be used, due to the greater and more precise expressivity. I have much greater confidence that my first pass at a piece of code will work correctly in Rust, once it compiles, than I do in JavaScript—and I’ve been working in JavaScript a lot more than Rust in the last couple of years, yet it’s still true.
- empath75 8y agoIt doesn’t. I didn’t say it did.
- peterhunt 8y agocertainly won't guarantee bug free code, but exhaustive pattern matching really helps when you're building compilers (as it does in other languages that have this feature).
- rubber_duck 8y ago>What part of rust prevents the writing of bugs Expressive type system, strong and static - JS is weak and dynamic. This is not limited to Rust - but if you're aiming for correct code pretty much anything after C/C++ and maybe PHP is better than JS and it's million and one way to write unexpected bugs and 10.000 ways to do the same basic thing (with an NPM package no less) of which each dev/library chooses it's own path for cool factor. Who would even argue that JS is better for writing correct code over a language that's obsessive about security and correctness. Last time I checked Rust wouldn't even upcast ints FFS, meanwhile in JS you can't even be sure what "this" refers to in any given context and 0 == '0' == [0].
- pythux 8y agoBabel makes extensive use of Flow. So I guess that counts as expressive static typing.
- eiurafhlfie 8y agoTagged union types (called `enum` in Rust), recursion, and pattern matching are great for tree data structures, such as ASTs, and help catch many bugs. This is the reason why so many compilers, typecheckers, and interpreters are written in languages like OCaml, F#, Haskell, and Rust.
- snaky 8y ago> OCaml, F#, Haskell, and Rust If you change Rust to ML, the phrase would be way more natural.
- n-gate 8y agoHow many non-toy non-self compilers and interpreters are written in Rust?
- steveklabnik 8y agoThere’s at least a few; cranelift is powering stuff in production at Fastly. Facebook is writing a MIR interpreter to do static analysis, don’t think that’s production yet though.
- bluejekyll 8y agoRust doesn’t implicitly create bug free code. I’ve created lots of bugs in Rust. But there are classes of bugs that the language eliminates. Thread unsafe dataraces (not important in JS), unhandled exceptions, with no GC it helps reduce memory usage and make it more consistent, the type system allows you to build complex state machines easily and correctly, the type system itself can allow you to express states that should never occur as compilation failures, the compiler protects against concurrent updates in iterators and other situations. The list goes on, but these are the things that make it such a powerful language and allow me to focus on logic and not runtime bugs. Oh, and it’s really fast which is a nice bonus, generally as fast as C.
- wwright 8y agoTo add: safe Rust prevents all data races, not just thread based ones (this is mostly relevant around iterators and other concurrency-without-parallelism structures, but is relevant more generally as well)