6 ms·
While Rust can indeed provide great benefits over C in many respects (mostly security and safety, but also maintainability), rewriting "everything" in Rust, as
by pron 10y ago
While Rust can indeed provide great benefits over C in many respects (mostly security and safety, but also maintainability), rewriting "everything" in Rust, as the author says, is so monumental an effort that there is no chance of it happening, certainly not before a "better" language than Rust comes along (say, in another ten or fifteen years), and then what? Rewrite everything in that language?
A far more feasible approach (though still very expensive) -- and probably a more "correct" one, even theoretically -- is to verify existing C code using some of the many excellent verification tools (from static analysis to white-box fuzzing and concolic testing) that exist for C[1]. It is more "correct" because, while perhaps not providing some of the other benefits, those tools can prove more properties than the Rust type system can, and such tools don't (yet) exist for Rust.
Even if you want to write a green-field project today that absolutely must be correct, it's better to do so in C (or in some of the verified languages that compile to C, like SCADE) and use its powerful verification tools, than to write it in Rust. Verification tools are usually much more powerful than any language feature (even though Rust's borrowing system seems like it can prevent a lot of expensive real-world bugs). Of course, it would be optimal if we could do both, but the Rust ecosystem simply isn't there just yet.
This is part of a more general problem, namely that rewriting code in new languages -- any new language -- is so costly (and often risky, too), that the benefits are rarely if ever commensurate with the effort. This is why good interop with legacy libraries is so important (something, I understand, Rust does well), often much more important than most language features. Rewriting code is often the worst possible way to increase confidence in a library/program in terms of cost/benefit, both because verification tools are often more powerful than any language feature, as well as because new bugs are introduced -- either in the code or in the compiler (bugs that won't be uncovered by the usually incomplete legacy test suite, but would break some of the millions of clients). Rewriting is usually worth the effort if the codebase is about to undergo a significant change anyway, but almost never to increase confidence in the existing functionality.
EDIT: Clarification: if you're not going to use verification tools in your new project, anyway, than obviously using Rust would give you much better guarantees than C.
[1]: C and Java are the languages with the best collection of powerful verification tools (maybe Ada as well, but there's far less Ada code out there than C or Java).
- fpoling 10y agoEasy interaction with other languages is a double-edged sword. Too much of foreign calls may lead to unmaintainable code when knowledge of both languages and their libraries is required to even understand what is going on. And of cause, it makes it even harder to use a whole-program static verification as the tool now must understand both languages.
- pron 10y agoTrue, but this downside is still orders of magnitude less costly than rewriting "all" widely used C code out there. This is good as a limited exercise to learn the strengths and limitation of a new language, but as a serious undertaking a gross misuse of resources. Besides, by the time it's done, there will be a new language out there, with the same downside you point out. To avoid it, the software industry would basically do nothing but rewrite legacy code over and over.
- fpoling 10y ago"No so easy to use" does not mean no re-use. For this reason I really like how Elm interacts with JavaScript. The language requires to define so-called ports that provide explicit bindings to-from JS world. Thus it is not worth the efforts to call into JS function that does some trivial stuff. But interacting with bigger libraries is straightforward. As a big bonus guarantees of Elm's type system survive interaction with those libraries (unless the JS code in those libraries is malicious), so one does no need to know the details of those libraries to reason about Elm code. Note that from this point of view rewriting C runtime library in Rust is not good as it creates too tight coupling between C and Rust worlds, requiring to write in a rather unnatural style in Rust. Besides, a lot of Musl essentially translates C calls into kernel ones when Rust typesystem brings no advantages.
- lmm 10y ago> It is more "correct" because, while perhaps not providing some of the other benefits, those tools can prove more properties than the Rust type system can Such as? And in that case, why not lift them into a language type system? When working on code where particular properties are verified, it's very useful if all the tools share a common understanding of those properties. If you write something in Rust, your IDE will understand the types (and make sure to preserve them when doing automated refactoring), your debugger will know which preconditions were supposed to be semantically impossible, and so on. Last time I looked at external verification tools for C-like languages you often ended up having to explain the same fact twice to two different verification tools with overlapping featuresets, yet alone getting any help from other tools. If we're going to reach critical mass on safety tooling, it will have to be via a commonly understood specification that's a de facto language definition. So given we're creating a new safe language, what parts of C would we actually want to keep? The lengthy operator precedence table? The preprocessor, and the absence of any module system? The extensive array of language-level operators for doing a wide variety of arcane bit-twiddling operations? The signed/unsigned arithmetic promotion rules that even experts misread? The implicit conversions between arrays and pointers? The lack of any sum (tagged union) type? The for loop that makes arithmetic - or, indeed, arbitrary code - first class, and iterating over an array or other collection something you hand-code every time? Even if it were as safe as Rust, C would still be harder to read, and harder to write as well. We can and should do better than C.