18 ms·
Why you should, actually, rewrite some of it in Rust
- kreetx 9y agoOr even better, Haskell!
- skybrian 9y agoIf you rewrote only part of a large C++ project in Haskell, is there a way to link it back in again?
- aeorgnoieang 9y agoCall Haskell from C (not C++) seems pretty easy: - [Calling Haskell from C - HaskellWiki](https://wiki.haskell.org/Calling_Haskell_from_C https://wiki.haskell.org/Calling_Haskell_from_C) And the FAQ on the Haskell wiki directly addresses this too: - https://wiki.haskell.org/Introduction#I_already_have_a_large_application_in_C_or_C.2B.2B https://wiki.haskell.org/Introduction#I_already_have_a_large.... So apparently it is doable!
- mrkgnao 9y agoHN supports a funky subset of Markdown. :(
- concede_pluto 9y agoOnly paragraphs, code literals, italics, and URLs. https://news.ycombinator.com/formatdoc https://news.ycombinator.com/formatdoc
- antientropic 9y agoIt pulls in a huge runtime system, though (including stuff like garbage collection). This makes it much heavier than calling some Rust code.
- marcosdumay 9y agoTrue, where "huge" means something around 10MB. For many projects that is a showstopper.
- mrkgnao 9y agoThe URL is slightly misleading: yes, you can call Haskell from C++. https://stackoverflow.com/questions/3859340/calling-haskell-from-c-code https://stackoverflow.com/questions/3859340/calling-haskell-...
- kreetx 9y agoIt definitely can, but if you're asking for an out-of-the box support, then it's probably not as easy to use as Rust. Then again, if you plan to rewrite your code base, then why not invest -- typed IO is a big thing.
- hashmal 9y agothe cool thing about rust is that you can use it for real-time programming, one example being DSP/audio. it often means no locks, nothing blocking, no dynamic allocation (malloc and most other allocators do not provide upper bounds of execution time), no recursion, and others. haskell is not tailored for that. of course there are other domains for which haskell is top-nocth and rust falls flat.
- AnimalMuppet 9y agoMinor nit: I believe that you can get memory allocators that provide upper bounds of execution time. But it won't be the default allocator in most languages. Also, you need an allocator that cannot ever fail to return a valid block. Even if you have all that, it still makes it much harder to prove that your worst-case timing is fast enough. So, yeah, no allocations in situations like that. Allocate at start-up, and re-use your buffers (for that kind of code).
- hashmal 9y agoI think there are memory allocators that provide bounds, yes, but I never bothered finding one. Time-constrained code is so demanding already that handling the memory part is just one drop in an ocean of constraints. (and the whole thing is actually very interesting to do!)
- crb002 9y agoC can be safe if you use Turing incomplete libraries that have been verified. It's the YOLO ad hock parsers that get you in trouble.
- sqeaky 9y agoI am not sure if you are serious or not. How does verification prevent buffer overflows? It can reduce their frequency, but you can never know you got them all. They find issues in libCURL and openSSL that have existed for years. Very few people are better than the people working on those libraries. It is better to be safe by default then add risk only when you need it. I say this as a C++ developer, I think const should be the default state of all variables and one should have to opt out of that, and Rust did that right and C is so far from that I see no reasonable way to use plain old C safely. At least with C++ I can hide my stuff in classes and verify one thing at a time.
- StavrosK 9y agoIt was probably the "Turing-incomplete" part that prevents buffer overflows. Easier to reason about/verify.
- snakeanus 9y agoFormal verification is not the same as a security audit. With formal verification you can prove that something is bug-free. > They find issues in libCURL and openSSL that have existed for years Neither of them are formally verified. https://github.com/seL4/seL4 https://github.com/seL4/seL4 however is formally verified.
- sqeaky 9y agoPlease, step away from "formal verification" and come back to world the rest of us devs work in. I have never seen a formally verified piece of software even once. I don't know if its too hard to do to real software or if C doesn't allow for it, or if it is just too expensive. The simple matter is that is not an option for the vast majority of projects. Unless you can make it practical, I assert it has no place in a practical discussion about preventing software issues.
- w8rbt 9y agoIf you are really worried about the security of old C code, then the safest, most portable thing to do is re-write in modern C++. This is sane and doable in a short period of time. Rust is not.
- strictfp 9y agoWhy not Rust? Given that you aren't very familiar with niether C++ nor Rust, I'm not convinced that C++ is saner or faster.
- sebcat 9y agoOr just rewrite it in modern C. This is sane and doable in a short period of time. You just have to make sure it's modern C. (no true Scotsman)
- camus2 9y ago> Or just rewrite it in modern C. This is sane and doable in a short period of time. What do you call modern C?
- sebcat 9y agoWell, anything that does not support the point I am making is not modern C.
- knicholes 9y agoI think his comment was facetious. I think the clue is "no true scotsman," which is a type of fallacy. I think the point is that it's assumed to be better because of the use of "modern," and that it's only valuable if it's rewritten in something modern. Perhaps it's a joke about how old code is actually fine, and that people who get excited about the cutting-edge technology could chill a little bit. Maybe it's just a mockery of the previous arguments.
- adwhit 9y agoNo true Scotsman: "We found a terrifying vulnerability in your codebase. You should rewrite it in modern C++." "OK, done!" <Time passes> "We found a terrifying vulnerability in your codebase. It must not have been modern C++!"
- hbbio 9y agoYou can also rewrite to C++, OCaml, Go... Each language has benefits and drawbacks, and there is good reason for each them to exist. Would your project benefit a lot from algebric datatypes -> OCaml is a great fit! You want to port as soon as you can: C++ is your friend. You plan to write HTTPS service nodes: Go has the best standard library out there.
- lobster_johnson 9y agoIf you read the article you'll notice that he proposes that Rust is a better language because it allows incremental rewriting, rather than a complete one; Rust doesn't have a runtime, doesn't have GC, and can fairly seamlessly/painlessly integrate with C (you can both call and be called from C easily). That changes the equation considerably, compared to something like Go where none of these benefits exists. For example, exporting code from Go not only requires the runtime and GC, there's also significant function call overhead because of the way Go maps goroutines to threads. There are many other good languages, but few of them have these benefits. C++ is probably the option with the least amount of friction against C, but then you're also inheriting most of the unsafety of C.
- rbehrends 9y agoIncremental rewriting introduces the potential for new issues, as foreign function interfaces are inherently unsafe. Rust also has its own costs. Its biggest limitation when it comes to mixing in code with that written in another language is probably its poor support for shared ownership (i.e. multiple references of the same object), while in most languages, shared ownership is the norm. Multi-language setups (which you invariably get with an incremental rewrite) also generally create other interfacing issues when they share objects. How are you going to represent `std::shared_ptr<T>` or `std::vector<T>` in Rust, for example? They are opaque types, after all. Can you guarantee that Rust properly observes C++ semantics with regard to assignment, move assignments, and destructors when dealing with instances declared entirely in C++ code once you start mixing and matching C++ and Rust?
- vvanders 9y ago+1 to everything said here. It really is very straightforward to start inserting Rust into a C/C++ codebase. I did the Rust port of netcode.io[1] which started out by just calling C code for the portions that I didn't have working in Rust. Eventually the port was 100% Rust but I was really impressed with how seamless mixing C/Rust was. Even now our unit tests run against the C code at a functional level so we know when the protocol diverges which lets us easily catch breaking changes from the reference C implementation. Much like everything, don't apply an adage blindly and do a proper evaluation but I think if you're in the a space where memory or performance matters Rust is a very strong contender that plays well with existing ecosystems. [1] - https://github.com/networkprotocol/netcode.io https://github.com/networkprotocol/netcode.io
- snakeanus 9y agoWhy Rust? Why not Idris? Why not Haskell? Or you know, why rewrite it at all? You could instead try and use a safe C implementation (such as gcc/clang with the sanitisers or something like https://staff.aist.go.jp/y.oiwa/FailSafeC/index-en.html https://staff.aist.go.jp/y.oiwa/FailSafeC/index-en.html), but to be frank if I was to rewrite my programs I would use a truly modern language like the ones that I mentioned above.
- Shriken 9y agoAll of your questions are addressed in the article. > Why Rust? Why not Idris? Why not Haskell? it can easily call C code it can easily be called by C code (it can export C compatible functions and structures) it does not need a garbage collector if you want, it does not even need to handle allocations the Rust compiler can produce static and dynamic libraries, and even object files the Rust compiler avoids most of the memory vulnerabilities you get in C (yes, I had to mention it)
- snakeanus 9y agoAll of these except GC also hold true for the languages that I mentioned. As for GC, I think that Mercury avoids its use https://lirias.kuleuven.be/bitstream/123456789/131304/1/Mazur.pdf https://lirias.kuleuven.be/bitstream/123456789/131304/1/Mazu...
- Rusky 9y agoNope- C interop is higher on the list for a reason. Idris and Haskell do not produce straightforward, runtime-less binaries that can be easily loaded by e.g. CPython or Node, without any interference with their runtimes.
- mhh__ 9y agoThese parts are true of D, and C++. IMO Rust's only true advantage over it's direct "competitors" is it's safety guarantees.
- SAI_Peregrinus 9y ago
- apenwarr 9y agoThe title ought to have been, "Why you should rewrite some of it in Rust." That's what the article is actually about. Then we could have a productive discussion about which parts, rather than yes or no.
- dang 9y agoGood idea. We've added that bit above.
- falcolas 9y agoSigh. This again. Not all security vulnerabilities are due to pointer arithmetic or out of bounds execution or pick-your-rust-is-better-idiom. Heartbleed is a great example. It wasn't caused by an error with how C handles memory or strings or anything else. It was caused by failing to validate untrusted input. Rust isn't going to help you with that. Could the affected parts be re-written in a way where the boundary checks would have worked? Yes. As they could have in C, C++, D, Fortran, or any number of other languages. If memory safety and correctness was so truly valued above everything else, we'd be seeing a lot more Ada than we do today. And this doesn't even cover the fact that not everything can be re-written in Rust, mainly due to compilation target restrictions. Exasperated Edit: No, I'm not saying code should never be re-written. I'm not even saying that Rust is a bad tool if the decision to re-write is made. What I am saying is that Rust is not a silver bullet. Rust is not going to solve every problem ever. Rust is one of many choices for writing memory safe, C ABI compatible code. The reasons provided by this article are insufficient to solely justify a re-write in Rust; the exact same arguments could be used as justification for rewriting everything in Ada. Or Haskell. Perhaps some thought will be put in the next "re-write everything in Rust" article that takes that into account. Then again, none of the past articles have.
- petters 9y ago?? Surely Rust would have helped against heartbleed? I mean, some servers were leaking private keys because they sent random segments of memory back to clients.
- gsnedders 9y agoThe problem is they implementing their own memory management on top of the standard primitives; they never sent unallocated memory to the client.
- the8472 9y agorust can also help you make custom memory management safer, e.g. there are several arena crates which hand out allocations with lifetimes, which means the arenas won't be freed before all references went out of scope. And there also is no pointer arithmetic into the arena, you only interact with individual allocations. I don't know if it would have prevented heartbleed, but it enables 3rd-party allocators to provide the same safety guarantees as the rust standard lib does. Something that cannot be said about C because there are no safety guarantees in C in the first place.
- ekidd 9y agoThe author focuses on rewriting low-level media decoders in Rust. I have bit of experience in this area. I've been slowly working on an MPEG2 binary subtitle decoder in Rust: https://github.com/emk/subtitles-rs https://github.com/emk/subtitles-rs A while back, I ran my subtitle decoder through "cargo fuzz", and I was pleasantly surprised at the results: Close to half a billion fuzz runs found 5 runtime panics, all of which were detected by Rust before they could compromise security. If I'd written this code in C, several of those errors would have been exploitable. I like to think I'm a lot more paranoid than the average programmer. But the MPEG2 format is gnarly and, sooner or later, I'll miss a potential overflow when bit shifting, or get confused following internal "pointers" in a subtitle packet. Rust has a few advantages for this work: 1. Rust does not require a garbage collector or other specialized runtime. This makes it far easier to pretend to be boring C code. This is a significant advantage over some other excellent languages like Haskell, etc., which require non-trivial runtimes. 2. Rust is very fast by default. For low-level programming, this matters. 3. The Rust infrastructure for testing and fuzzing is surprisingly good and easy to use, which makes it easier to produce bullet-proof libraries. The downside is that even if you're already familiar with C++, it's probably going to take a couple of weeks to become comfortable with Rust. And if you don't really understand stacks, heaps, memory layout and references, it may take even longer. I do agree with the underlying thesis: In 15 years, I'll be heartbroken if we're still facing an endless stream of security updates and remote root compromises. But it's going to require literally billions of dollars of programmer time to put a dent in this problem.
- yjftsjthsd-h 9y ago> Close to half a billion fuzz runs found 5 runtime panics, all of which were detected by Rust before they could compromise security. If I'd written this code in C, several of those errors would have been exploitable. Wouldn't the apples-apples comparison be fuzzing a C program? If AFL would catch the same bugs, Rust isn't better than C in this case.
- Jweb_Guru 9y agoThe difference is not in what was caught by AFL, but what wasn't (and also, what would have happened in C had AFL not been run).
- lj3 9y agoHas anyone done any in depth explorations of the safety and security of rust code vs well written C or C++?
- hashmal 9y agoit's all in the "well written" part. of course well written C/C++ is good. the very point of Rust (and others, like Ada) is to admit that we, programmers, cannot do good work all the time. anyone thinking "I'll just write these 50k SLOC very well so it'll be ok" should not consider a career in programming.
- sqeaky 9y agoWell written the hard part there. I am riding on on the idiomatic C++11/14/17 train right now. Life over here is great, but I can the warts of the language looking backwards. The promised land in the future doesn't look quite as bright as the promises Rust offers. The only thing keeping me from trying it is the giant codebases in my current projects.
- pjmlp 9y ago> I am riding on on the idiomatic C++11/14/17 train right now. How do you see it? Nowadays I only use C++ on hobby projects or when Java/.NET need some kind of integration work. To me it seems that as code bases grow organically, it will become very hard to get which code is in which ANSI version, specially problematic given some of the changes that took place between standard revisions.
- sqeaky 9y agoAt both of my jobs, I am working way too much, I am working in pre-C++11 codebases which now allow C++11. Both are working to for -Werror everywhere and that helps a huge amount, and we can try to get more warning listed and make broad but subtle changes to the codebases. At one place we are in a race before a tight deadline and at the other we are breaking a large monolith into smaller packages. At one place we implement a new practice for all our code, and at the other we implement all our best practices as we chop code out of the monolith. Both places are seeing a reduction in bugs. The place with the packages is probably reaping a larger benefit because they are aggressively re-working old code and have better test coverage and can do so with confidence. Code bases do grow organically, but like places they can be tended and pruned to make impressive orchards or topiaries or left to the wild to become impassible jungles. Tools like RAII, exceptions, classes, etc... are just tools for managing this. I think the tools in C++11/14 are at least as powerful as in any language except perhaps Rust. Every time I hear a C developer say "C can be just as safe..." I am imagine a person with nothing but a pocket-knife trying to manage a whole forest.
- dman 9y agoAm waiting for Coq developers to start showing up and asking for Rust projects to be rewritten using Coq / Compcert.
- snakeanus 9y agoSadly Compcert is not foss, meaning that they will have a huge problem if they try to push it everywhere. I think that the age of non-free languages and implementations meant for the general public is over, even MS has made their C# and F# implementations free.
- Jweb_Guru 9y agoCompcert is guaranteed to compile correct C code to correct assembly (given its TCB assumptions). It doesn't say anything about incorrect C code. So in order to benefit from CompCert, first you have to prove that you have valid C in the first place, which is no easy feat. You'd probably be better off using a language that's designed with verification in mind, like Cogent or ADA/Spark, or waiting for lambda-Rust to become production ready, since in those cases you won't have to prove tedious bureaucratic memory safety invariants for the vast majority of your code, and can focus on functional correctness (or, if you just care about memory safety, you can stop there).
- grabcocque 9y agoIs this article "Use Rust" or "Stop using C"? I mean both are reasonable arguments that get made often on here, but the one doesn't necessarily follow from the other.
- EugeneOZ 9y agoFirst
- pm90 9y agoI think its a little bit of both. Primarily it is aimed at legacy software. IMO not a lot of new code is written in legacy C, so we have a problem with having a ton of dependencies written in a way that may not be the safest or most reliable (from recent experiences). The authors suggestion is to rewrite parts of those applications that tend to be buggy (mostly I/O parts) in Rust. I really hate to see the naysayers comment on this article without even having read it. I think the author made a very good point.
- faragon 9y agoIn my opinion, there are more reasons to rewrite Rust code in C.
- ndr 9y agosuch as?
- faragon 9y agoE.g. ABI compatibility, multi-platform support, tools, open source code base, community, etc.
- fuwafuwa 9y agoABI compatibility: Not really solved in C++ - see MSVC versus Mingw. If you're coming from the standpoint of using a Linux distro or BSD you enjoy a sanitized environment from the start. Multi-platform support: Pray that your project's build system makes it straightforward to get all the dependencies going on every platform. Pray that your dependencies are easy to access and of the correct version. Pray that the manufacturer's fork of an ancient gcc isn't too buggy to use. C made it possible, not easy. Tools: Both languages are too impoverished to have tooling as comprehensive as the stuff available in, for example, Java, although VS pushes very hard on this front. rustc already gives better error messages than any C++ compiler. Code base: See the build system problem. Maintaining open source projects in C and C++ is an exercise in endurance. Single header file libraries have come into vogue as a result - and they're a hack. There is a lot of decent code in both languages not being used because it's hard to get running. Community: Rust is in a better position here in part because it's smaller. If I encounter a C++ hacker in an online space it's just as likely to be an aggro kid as a seasoned pro.
- faragon 9y agoMy point was explicitly about C, not C++. All these points are solved in C, decades ago.
- Syzygies 9y agoOr write in Haskell in the first place. Good Haskell code is rather fast, but that's beside the point. For many projects, the only measure of goodness should be how nimbly one can then use and experiment with one's code. In Haskell, like Lisp before it, one ends up with a language to use one's code. Like writing a Mathematica package and getting all of Mathematica for free, only better.
- _dcwr 9y agoAs someone who really likes C and C++ I feel like the biggest reason for me not even thinking of Rust as a viable language right now is the community attitude. It seems so damn hostile to C and C++. Anyone who thinks C and C++ is viable is a misguided idiot, bad programmer, writing unsafe software, etc. to these people. Some badass below even just said that "writing C++ code longer than a few lines without UB is humanly impossible". Apparently all these FUBAR C/C++ systems (Linux and Windows kernels, all? web browsers, LLVM that the dear rust uses, device drivers, web servers, router software, VMs of Lua, Python, C#, Java, ..., etc.) work well enough to transfer that kind of garbage to me. It's a bit like the decades old vim vs. emacs, C vs. C++, C++ vs. C# vs. Java, etc. feuds.
- vvanders 9y agoI think you see some of that since quite a few people have spent decades in C/C++(myself included) and have had exposure to experiencing these exact problems. I'm still a huge fan of C++ but Rust gets me to where I want to go with C++ with much less fuss and a lot more confidence.
- FRex 9y agoI'm a big fan of Python lately but neither I nor Python community at large goes "you fucking scrub, use Python, fuck you, wtf are you doing, stop making broken progs for ppl and wasting your time, omg, obsolete shit you use" and instead highlight upsides of it like readability, plentiful modules, widespread use of it, etc. Meanwhile Rust community seems full of negative advertising in the tone of "fuck C, fuck C++, Rust is safer and better, always, for everything". I see this every time Rust is brought up, without exception. Like WTF? I can understand people disliking C, C++, Lua, whatever, but not going batshit insane over them and saying you need stars to align to write a C program that isn't broken and that to write an unbroken C++ program is impossible...
- curun1r 9y agoI think what you're seeing is less the Rust community and more the community of computer users that have been forced to use C/C++ software for decades now. Up until Rust, there were many easy reasons why developers could justify their choice to use C/C++. And so users had to live with the frustrations of running code written in those languages because there really were few well-known alternatives. Now Rust comes along with its safety claims (which are mostly true) and suddenly these computer users have what they believe is a legitimate complaint. They now see the frustrations with running C/C++ software as no longer facts of life but, instead, development externalities that users are forced to pay based on the decisions of the developers. And so you have a bunch of people who've likely never written much in either C/C++ or Rust who are trumpeting Rust because they're sick of dealing with CVEs, segfaults and all the other 1970s baggage that the C-cosystem imposes. And it doesn't help that C/C++ developers tend to react defensively and take the advocacy as an impugnment of their developer honor. Saying "It's only inept C/C++ developers that write code with UB or memory safety issues. Disciplined developers can write safe C code!" just infuriates people even more since the weight of evidence suggests that any non-trivial C program will eventually have a bug or vulnerability of this kind. Humans make mistakes. That's an immutable fact that we need to take into account when relying on humans to do anything. Programming is a thing that is part of that anything. To err is human, to catch as many of those mistakes as possible is the compiler's job. But I think both the Rust community and the Rust-resistant community should be wary of those trumpeting Rust who are part of neither of those groups. There are many good reasons to use Rust and some good reasons not to. Often times a hybrid Rust/C solution would be the best. We should be careful to try to take ego out of the decision making process as much as possible. And we should remember that when users are asking for Rust, what they're really asking for is an end to the pain that they've had to deal with. If it can be accomplished in some other way, then that works too.
- Animats 9y agoThe trouble with mixing Rust and C/C++ is that you tend to get Rust with C/C++ unsafe pointers. Most of the examples of this pass C raw pointers into rust, Rather than passing a Rust Vec into C. Or even passing STL strings and vectors into Rust. So, instead of passing Rust's array size information into C, they propagate C's lack of size information into Rust. Typical example: [1] pub extern "C" fn count_substrings(value: *const c_char, substr: *const c_char) -> i32 If you incrementally convert from C/C++ to Rust, you end up with those constructs in the final Rust program. Not good. [1] http://siciarz.net/24-days-of-rust-calling-rust-from-other-languages/ http://siciarz.net/24-days-of-rust-calling-rust-from-other-l...
- dbaupp 9y agoYou only end up with them in the final program with a naive translation that makes life hard for yourself. Using the native types is much nicer (even ignoring any safety benefits), and so people are naturally encouraged to do so. For instance, - the C interface can easily be thin wrappers around pure "nice" Rust functions, that just do the necessary unsafe conversions - and, whether or not that is used, when a function is no longer being called from C (that is, everything that calls it has been converted to Rust) the arguments can be switched to something more natural and strong static typing will point out all the places that need to be updated. I cannot believe that a pure Rust codebase would want to be passing raw pointers around. And the alternatives don't seem at all better: only ground-up complete-rewrite migrations (hard to justify so often won't even start) or not migrating at all (meaning one has a C codebase that is still full of unsafe pointers).
- kronos29296 9y agoWell Rust seems like a nice enough language if blog posts are anything to go by. But I really cannot unless we something like RustPython and Rython to replace CPython and Cython. (If anybody does write a new Python distribution on rust I named if first)
- cbcoutinho 9y agoSomeone has already done the honors: https://github.com/shinglyu/RustPython
- kronos29296 9y agoShould have seen it coming. Though it seems to be a one man show for now.
- mtgx 9y agoWe need Rust to have great support for ARM microcontrollers before the IoT industry really takes off.
- biokoda 9y agoThe relatively low portability of rust programs is definitely one of its largest shortcomings.
- bboozzoo 9y agoCan you elaborate? Although haven't tried it myself, I was under the impression that it's quite possible to write low level code for at least for x86 and ARM (and AVR?).
- biokoda 9y agoYou can write low level code. Tier 1 platform list is very short however. https://forge.rust-lang.org/platform-support.html https://forge.rust-lang.org/platform-support.html
- wtallis 9y agoIt's probably unreasonable to expect a wide variety of IoT platforms to meet the "automated testing" requirement for Tier 1 status.
- steveklabnik 9y agoWhat aspects of support are you missing today?
- ZoFreX 9y agoIs it really the time, yet, to start advocating for rewrites in Rust? Worth considering for new projects, definitely. Using to write new components of existing projects, maybe. But rewriting existing work? I'm concerned if we start pushing that too much too early, the initial friction will burn people. You only get one first impression. For anyone who thinks "yes, now is the time" I have questions for you: Have you released and distributed software written in Rust? What's the install story like for end-users? Is it in a repository for a major OS (e.g. can I yum install it, or apt-get it) and if so which? As far as I can tell there's no major package manager accepting or even ready to accept programs written in Rust yet. Am I misinformed?
- dikaiosune 9y agoI believe Firefox as of recent versions requires a Rust compiler to be available, and that's a pretty bread-and-butter application to have in your distro's package repository.
- Paul-ish 9y agoYou should only need a rust compiler to compile Firefox.
- dikaiosune 9y agoMost distributions require that packages in the repositories are built with tools from the distribution.
- steveklabnik 9y ago> As far as I can tell there's no major package manager accepting or even ready to accept programs written in Rust yet. Am I misinformed? At least Debian, Fedora, Arch, and Gentoo have rustc and cargo in their repositories. Oh and Alpine, recently. FreeBSD (IIRC) has it in ports. In the not-too-distant future, there's a pretty important package that will require Rust: Firefox. HEAD already does today.
- 9y ago
- nercury 9y agoWhile I am avid supporter of Rust, I can't help but feel this post is not right. If Rust is so much better, why not prove it by writing software in Rust, instead of words. After all, if Rust is so much better, the result will speak for itself.
- dikaiosune 9y agoIn this case it seems the author is writing about the concrete results they've already obtained, not about something abstract.
- youdontknowtho 9y ago"Rewrite it in Rust" is the new "Stallman was right" thread. Its an echo chamber of fandom. Over time I've come to despise "evangelism" or any kind. I'm sure Rust is great. I just don't think it's news every time a Rust fan says that its great. YouTube lectures of people actually using it? That would be interesting. Interviews of people actively porting a large C project to Rust. That would be interesting. Rehashing its feature sheet? Not so much.
- dikaiosune 9y agoThe author is actually using it, to actually rewrite portions of C projects in it, and writing about their experience doing this in practice. For example, the linked paper is about using Rust at the I/O boundary in an existing, complex system.
- youdontknowtho 9y agoExcept that the article is actually just the feature sheet of Rust. I would rather the article had been a link to that paper.
- dikaiosune 9y agouhhhhhh, multiple links in this block on the post: "That’s because I have been working on this subject for a long time now: * I did multiple talks on it * I even co-wrote a paper * I did it both as client and personal work"
- youdontknowtho 9y agoYes, I saw those links. The article that op linked to though was another Rust feature sheet blog post, though. This was days ago, why do you care?
- FRex 9y agoOne of the reasons why I'm super hesitant of learning Rust is the attitude and emotional based opinions of the community. I picked up Python due to my work and I love it and I'd never think I would before trying it. But I also never in my travels seen Python people advertise it negative with "<somelang> SUCKS! FUCKING IMPOSSIBLE! GIT GUD AND USE PYTHONN!" about ANY widespread or niche language but instead they just keep positively gushing over Python: "how clear", "how succinct", "formatted by significant whitespace", "readable", "simple", "pythonic", "wide array of modules available", etc. When someone in one message both recommends Rust and says that it's absolutely impossible to ever write correct programs in C or C++ that are more than few lines long what am I supposed to take away from that? Rust itself uses C++ written LLVM, is it unreliable too then? Seriously... And implicitly, anyone liking C or C++ is an idiot and a bad programmer.
- pornel 9y agoI've been programming in C for 17 years. Now I'm writing (and rewriting) image processing and encoders in Rust and I love it. It's not just memory safety. Rust has sweet error handling — the syntax is almost as noise-free as exceptions, but code flow is as predictable as error codes. In Rust I handle rare error cases that I wouldn't have bothered with in C (and thus my Rust programs fail in orderly fashion instead of running with garbage values). Runtime arithmetic overflow checks have saved me many times. Knowing I have this safety net I can be cavalier about using smaller integer types. I don't have to free memory explicitly, and yet peak mem usage is as minimal as it can be, and I did not have a leak yet. And the whole ecosystem is fantastic compared to C. There's a culture of having unit tests. Ease of using dependencies via Cargo, and the zero-cost abstractions, help having dependencies broken down into small and focused libraries.
- CalChris 9y ago> the syntax is almost as noise-free as exceptions No. const USAGE: &'static str = "usage string"; I did rewrite it in Rust. I rewrote a small low level command line app in Rust. I really wanted to like Rust going in but I freaking hated it going out. They had a few good ideas but they never knew how to say no. The part I hated the most was the Rust macro language. And for real low level to the metal systems programming, everything is unsafe; so you may as well be writing in C. Go is pretty minimal. They knew how to say no. I like modern C (C99 and C11). It has most of the good ideas from C++.
- steveklabnik 9y agoAs of the most recent release, that's now const USAGE: &str = "usage string"; and there's talk of maybe making it const USAGE = "usage string"; > The part I hated the most was the Rust macro language. We don't like it either; there's a replacement coming. We reserved the "macro" keyword before 1.0 and made the current one use the less-good "macro_rules" to help ease the eventual transition.
- GrayShade 9y ago> We don't like it either But.. but.. Scheme macros are so nice!
- idyllei 9y agoI would love to learn Rust, but the learning curve seems a bit too steep for me. The whole struggle with lifetimes is what has kept me from diving in. It seems impossible to me to figure out how to use them correctly. Plus the redundant syntax, I guess. If those things weren't an issue for me, I would gladly try to learn Rust.
- steveklabnik 9y agoLowering the learning curve is a huge objective of this year! > The whole struggle with lifetimes How did you try to learn them? I'm interested in trying to make this easier! > the redundant syntax Which bits are redundant?
- jeffdavis 9y agoI am a fan of rust, but this article overstates the simplicity of C/rust integration. A few examples: * You may need to use PhantomData in your struct definitions to properly track lifetimes. Not sure I fully understand. * C functions don't always return. Sometimes they longjmp (c.f. PostgreSQL). Calls into C code need to prepare for this and it's not trivial nor does it generalize to all projects. * Integrating with the memory management scheme is probably possible but not trivial. For instance, Postgres memory contexts (regions/arenas) can hold any kind of object, and code can switch between them and create/destroy them at will. You could rely on rust to manage it's own memory, with circular references an Rc that might leak. * Postgres has its own calling convention for SQL-callable functions and conventions for loadable modules. Integrating with this is a pain because macros are hygenic and can't make up new symbols. Maybe procedural macros will help when they arrive. * Rich APIs that rust supports don't always translate well to C. When integrating with a large application that pushes you toward the lowest-common-denominator APIs. * A lot of C macros don't translate well to rust and just including a C header in rust is a lot of work. Hoping procedural macros will help here too.
- anarazel 9y agoBtw, are you planning to work on a thin integration layer between rust and postgresql? Sufficient to be able to write UDFs in rust? That's imo the most realistic start to get some experience w/ rust in the postgres community, and it provides value on its own.
- jeffdavis 9y agoI've made some progress. Not quite as thin as I had hoped ;-) I really need procedural macros to stabilize, otherwise it just puts too much pain on the UDF author.
- anarazel 9y ago> I really need procedural macros to stabilize, otherwise it just puts too much pain on the UDF author. Why exactly is that? I'd have expected a C wrapper to take care of most of that? Are you concerned about the Datum<->native type C macros? Are you planning/hoping for this to be all safe, or is there going to be unsafety in the wrapper?
- Const-me 9y agoWriting manually-vectorized SIMD code is the only way to approach advertised performance on any modern CPU. While there’s _some_ SIMD support in Rust nightly builds (but not in Rust stable), it’s just not good enough. For one thing, Intel only supports their intrinsics for C (C++ also gets that ‘coz compatibility) and Fortran. Another thing, Rust strong type-safety concept complicates SIMD code, especially when the code does integer math. These __m128i / __m256i / __m512i registers are often treated as different data types by even consecutive instructions. While Rust might be good enough for some areas, performance critical CPU bound code is not one of them.
- steveklabnik 9y agoYeah, SIMD is being very actively worked on; until then, you can link in an external assembly file, but that's not exactly the best UX.
- Const-me 9y agoYep, assembly’s not the best UX. When you’re coding assembly, you have to code everything in it, including code running on the scalar parts of CPU cores. With intrinsics you can code these parts the usual way, e.g. for( size_t i=0; … ) With intrinsics you can even compose your vectorized code the usual way, VC and Clang have __vectorcall calling convention for that, Intel has very similar __attribute__((regcall)).
- steveklabnik 9y agoYup, absolutely. My understanding is that an RFC is currently being written, so it's gonna be a little while to get into stable, but not forever. We'd have liked to have had it sooner, but some stuff happened, that's just how it goes sometimes.