17 ms·
Why I think Rust is the "language of the future" for systems programming
- adambratt 14y agoGood article! Sorry to be nitpicky but the font size/line-height makes it really hard to read. Perhaps make both a bit bigger for optimal reading?
- kumarshantanu 14y agoProbably this would help :) http://viewtext.org/article?url=http%3A%2F%2Fwinningraceconditions.blogspot.in%2F2012%2F09%2Frust-0-index-and-conclusion.html http://viewtext.org/article?url=http%3A%2F%2Fwinningracecond...
- kibwen 14y agoI just found this Firefox add-on today and I'm completely in love: https://addons.mozilla.org/en-US/firefox/addon/clearly/ https://addons.mozilla.org/en-US/firefox/addon/clearly/ Here's what appears to be the Chrome version: https://chrome.google.com/webstore/detail/iooicodkiihhpojmmeghjclgihfjdjhj https://chrome.google.com/webstore/detail/iooicodkiihhpojmme... Makes it ridiculously easier to read basically everything on the web.
- cpeterso 14y agoI like the Readability add-on and bookmarklet: http://www.readability.com/apps http://www.readability.com/apps
- drivebyacct2 14y agoRust and Go provide me with more day-to-day pleasure than... well... a sadly large number of things. This is a good resource for learning more Rust, thanks.
- kibwen 14y agoOut of mild concern over the title (not that it ought to be changed, TFA doesn't really have a meaningful title), I'd like to preemptively defuse any potential flame war. Go and Rust are not really competing. There may be some overlap in domain, but they occupy different niches and will likely appeal to different crowds. Go will appeal more to people who prefer its focus on conceptual simplicity and its "opinionated" nature (very much like Python). Rust will appeal more to people who prefer "functional" trimmings (algebraic datatypes, pattern matching, liberal use of higher-order functions, Scheme-style macros) as well as folks fed up with C++ who aren't willing to give up complete control over memory and performance characteristics. The fact that both languages are built around a similar concurrency model is just convergent evolution. It's tempting to cast Go vs. Rust as Google vs. Mozilla and/or Chrome vs. Firefox, but there's no practical reason that both languages cannot peacefully coexist and thrive.
- zheng 14y agoDoes Go not have higher order functions and pattern matching? If not, are they on the roadmap?
- kibwen 14y agoI'm sure that Go has higher-order functions, I just mean that Rust encourages you to use higher-order functions all the time. For example: in Rust, `for` loops desugar to higher-order functions (it's very neat, actually). Furthermore, whereas in Go you spawn a goroutine with the `go` keyword, spawning a task in Rust is done by handing a function (usually a Ruby-esque closure) to the `task::spawn` library function. (Note also that "higher-order functions" are not the same as "first-class functions".) Go does not have pattern matching, to my knowledge, though without algebraic datatypes I don't think it's really a big deal.
- wonderzombie 14y agoGo does not have pattern matching. There are type assertions, but obviously that's not the same thing. Go has higher order functions in some sense, and the standard libraries use them for some things. But I wouldn't say they're nearly as ubiquitous as they are in Ruby or your average functional language. My read is that Go is pretty firmly grounded in imperative-land. Regardless, since Go 1 came out relatively recently, I doubt they're going to make substantial changes to the language anytime soon. Obvs I don't speak for them, etc etc.
- megaman821 14y agoRust is what I had hoped Go would be. Google employs some of the brightest computer science minds in the world and turns out stuff like Go and Dart, which seem to be more aimed at enterprise Java programmers rather than computer scientists or programming enthusiasts.
- tptacek 14y agoWhat do Go and Dart have to do with each other, besides their origin at one of the largest tech companies in the world? Have you written much Go? What do you think of it in practice?
- megaman821 14y agoAs languages, nothing. In design, their conservative nature.
- cmccabe 14y agoGo is not a conservative language. A conservative language would be something like D, which is mostly a clone of C++ with a few extra features tossed in. Go is a highly opinionated language with at least three big new ideas: * goroutines for concurrency ("Don't communicate by sharing memory; share memory by communicating") * a new type system which is based on structural subtyping (some people have called this static duck typing). * use of return codes rather than exceptions for routine error conditions ("We don't want to encourage the conflation of errors and exceptions that occur in languages such as Java." See https://plus.google.com/116810148281701144465/posts/iqAiKAwP6Ce https://plus.google.com/116810148281701144465/posts/iqAiKAwP... ) Just those three things are enough to make Go code look very from what came before. And I could go on-- things like the way go does namespaces and scoping are also very different. It's a different philosophy that leads to much better code, in my opinion at least, not dozens of gee-whiz features, which seems to be what the languages of the 1990s gave us and are continuing to give us.
- pacala 14y ago
- Calamitous 14y agoHas anybody heard if they've finalized a spec yet, or at least come close? I've thought about getting into Rust from time to time, but the lack of a finalized spec and/or stable reference implementation has always scared me off.
- pcwalton 14y agoWe're getting close. Rust 0.4 (hopefully to be released next week, assuming no more schedule slips) will be quite close to final syntax-wise. Although not all the features will be implemented, your code will be mostly backwards-compatible. If you'd like to try out the language, I recommend working off git master rather than 0.3; the language has been dramatically simplified in Rust 0.4.
- Calamitous 14y agoExcellent, thanks for the update! :)
- brson 14y agoThere is no finalized spec (and the spec has actually fallen quite far behind for the moment), nor do I expect anything about Rust to be called 'final' for a while yet. The upcoming 0.4 release (any day now) makes a big push to get a lot of the syntax pieces in place, but still has many rough edges. Beyond that the standard library is still an incomplete and inconsistent stew of different styles. Rust is still firmly alpha software, but should stabilize significantly in the next year.
- rokekr 14y agoRust feels more like Perl 6 (always on the horizon) or Ada (the do everything language), while Go already is already specified, being improved, and out in production.
- nagnatron 14y agoI don't think it's like that. I think they're just developing it through usage. And it's not like it's been ten years yet.
- thisisstupid 14y agoI cannot wait to go and explain to senior management how their new and shiny VM deployment tool is written in Rust. It definitely won't raise any eyebrows.
- jallmann 14y agoI see Rust has ADTs and strong static typing, how much is enforced at compile time? One thing I love about OCaml is its exhaustiveness checking. I've been doing a lot of Erlang and it feels so dangerous without the compiler telling me if I've missed a pattern, or if a clause might return the wrong type. (Dialyzer and typespecs help somewhat, but aren't nearly as nice.) If I could have that sense of safety with Rust, I would be persuaded to investigate it sooner rather than later.
- bblum 14y agoShort answer is "all of it"[1]. 'match' statements in rust also exhaustive. I believe ADTs are used and checked exactly the same way as in ocaml (I've used SML and Haskell and they are the same as there). [1] not to say dynamic failure is totally gone from the language -- but it is if you avoid both writing 'fail' and using library functions that can fail. A common example is option::get(Option<T>) -> T, which fails if the optional value is None -- it leaves a sour taste in my mouth whenever I use it, but I still do sometimes.
- mpolun 14y agoRust is a very cool language, but I've never been able to get all of it in my head. Maybe part of that is because I've never actually used it, but it seems quite complex. Maybe it's not an issue in practice, and maybe there are just not good enough docs yet, but it makes me wonder if it will be worth the complexity in the end unless your alternative is C++.
- erichocean 14y agowhich is especially important in the multicore world where heaps must be protected by a global lock The article seems nice, but this is misleading at best, and flat out false at worst. Concurrent memory allocation has been around for many years, and gasp is perfectly usable with C.
- bblum 14y agoI wondered how long it would take for somebody to call me out for that. It seemed like a sketchy claim when I wrote it (it wouldn't be too complicated to make the @-heap lock-free in rust, for starters), but in retrospect, I shouldn't have said it at all. Thanks for pointing that out.
- bblum 14y agoI tried to think of an alternate justification, but couldn't come up with a non-awkward way of saying it, so just redacted that part entirely. I'll say here, though, a few reasons I think avoiding malloc/GC is important: - pcwalton has mentioned his desire to be able to reason about when the garbage collector runs, so that he can statically guarantee that the graphics rendering task in servo will never take a big pause to do a GC. - In this-must-never-fail environments, such as writing kernel device drivers, using dynamic allocation has to be avoided (and, if i'm not mistaken, even more likely to fail when done in an "atomic section" using kmalloc(GFP_ATOMIC)). Rust in its current state is not suitable for kernel-level development, but it's not far off; and it's critical to be able to reason about memory allocation for such applications.
- kaffiene 14y agoThat comment about GC in graphics sounds like BS to me. Java manages to handle situations like graphics rendering & GC perfectly well. You simply choose a GC that doesn't take large pauses. Graphics isn't an environment where you prove or statically guarantee performance a priori ('statically guarantee') - you run, profile and modify till it works. What's more, certain GCr's can make guarantees about not taking large pauses. p.s.: this is no criticism of Rust - looks very interesting and as a C/C++ programmer, I'll definitely be taking a look at it.
- zobzu 14y agoI don't know if it is the language of the future, but Rust is definitely high on my "let's reprogram Singularity/Plan9/Friends"-and-actually-use-it list I'd certainly join and attempt to help any project started in that direction, at least.
- mburns 14y agoMakes me wonder if anyone is working on a llvm-to-rust compiler.
- eholk 14y agoIt's interesting that you mention Singularity, as it turns out that Singularity has been a major influence on Rust. For example, the current communication is very similar to the channel contracts used in Singularity.
- zobzu 14y agoWell I'm a big fan of many concepts of Singularity. And I like rust. I guess that's no coincidence ;-)
- mappu 14y agoHow do you implement closures entirely on the stack? What about the case when a function returns a closure into an external translation unit? How do you cover the bound parameters? You can't do call-time lambda lifting if the bound parameters are out of scope at the call site, you need to put something on the heap and GC it. Same with compile-time lambda lifting (producing a chain of bind1st stubs on the executable heap)
- bblum 14y agoMy mistake. I meant to write "can SOMETIMES be entirely on the stack" -- fixed that just now. Functions indeed cannot return closures without heap-allocating them. (It might be possible in specific obscure cases, but definitely not in general.) However, if you wish to curry a function foo(a,b), you can do that in the caller (instead of at the function definition itself) by writing: let curried_foo = |b| foo(a,b); That is then stack-allocated and can refer to 'a' in the same frame, and can be passed to other functions you might call from there (but not returned upwards, indeed).
- kibwen 14y agoI'm not an expert, but I believe that Rust will allocate closures depending on the data that they close over. So if you reference a stack-allocated variable from within your closure, it gets put on the stack. If you reference a heap-allocated variable from within your closure, it gets put on the heap. I could be dreadfully wrong, though.
- pcwalton 14y agoNo, where a closure is allocated depends on the type of the closure. The type of the closure is inferred just like any other type. It has nothing to do with what it closes over (although what it the closure is allowed to close over depends on its type).
- james4k 14y agoThree different types of pointers? Perhaps that is going a bit far? > If you've a sharp eye, you're wondering what that "~" is that I snuck in on the type of the closure for the child task. That's actually a pointer type, of which Rust has three (none of which can be null, by the way): > ~T is a unique pointer to a T. It points to memory allocated in the send heap, which means data inside of unique pointers can be sent between tasks. You can copy unique pointers, but only by deeply copying (otherwise they wouldn't be unique!) (and by default, they are "non-implicitly-copyable", so the compiler will issue warnings if you copy them without writing the "copy" keyword). > @T is a managed pointer to a T. Currently, these are reference-counted and cycle-collected (they may be full-on GCed in the future). Copying one increments the reference count, so multiple managed pointers can point to the same data. These are allocated on a per-task private heap, and cannot be sent between tasks. > &T is a borrowed pointer to a T. It can point to the inside of arbitrary data structures - on the stack, inside ~ or @ pointers, etc. Rust has a static analysis, called the "borrow checker", that ensures that borrowed pointers must not outlive the scope of the pointed-to data (i.e., it is impossible for rust programs to have a use-after-free).
- kibwen 14y agoIt's not too daunting. `&` is your go-to pointer for most scenarios. `~` pointers are for anything you want to send to a different task. `@` pointers are for things you want to be garbage-collected.
- rohshall 14y agoFantastic summary of the pointers. It's less daunting now.
- scott_s 14y agoDo you have reasons why it's going too far, or is that just an emotional reaction? Consider that these pointer types map exactly to the pointer-type templates provide by C++11: unique_ptr, shared_ptr and weak_ptr: http://en.cppreference.com/w/cpp/memory http://en.cppreference.com/w/cpp/memory In the interest of starting discussion and not just stating facts, I will take the position that I think Rust's adoption of these concepts into the language is a Good Idea. Many pointer-based codes have these pointer types, but they are enforced through convention alone. C++11's templates are a step in the right direction, as they make explicit what convention assumes, but the compiler does not know about them. Yes, because they are template classes, there are certain things one can and cannot do with, say, a unique_ptr, but the compiler cannot do any analysis on the usage, because the concept has no representation in the language semantics. (Ben references a blog post by Niko Matsakis about some of the analysis Rust does: http://smallcultfollowing.com/babysteps/blog/2012/07/19/yet-another-tutorial-on-borrowed-pointers/ http://smallcultfollowing.com/babysteps/blog/2012/07/19/yet-...)
- elchief 14y agowhy didn't they call it YAPL?
- haberman 14y agoI'm a die-hard C guy. My motto for years has been "you can pry pointers and address spaces from my cold, dead hands." Of the new languages I've seen lately, Rust is my favorite. I love how it gives me better ways to express things I actually want to say without imposing GC on me. But even so, I can't see myself actually using it for much, because writing in a language other than C means buying in to that language's runtime. Buying into one language's runtime means that your code won't play nice with other languages' runtimes. If I write a library in Rust, how can I expose my types and algorithms to Ruby, Python, Lua, etc? How will Rust Tasks play with Python threads? What if I use a Rust Pipe to send a Python value between tasks? How do I keep Rust from doing a GC pass while I'm holding the Python GIL? etc. etc. Programming Languages by their nature want to be at the center of your world. If you buy into their abstractions, everything works nicely. But if you try to mash two of them together in a single process, you start to suffer from the fact that their abstractions overlap and don't interoperate at all. If you're only writing an application (ie. not a library) and never want to embed other languages into your application, then this might be ok. But I'm more interested in writing shared functionality that is useful across languages. Why should the whole stack of parsers, crypto, compression, etc. have to be written separately in each language? Life is too short to do some great work that is only usable by one language community -- computing is so big and changes so much that one-language-only functionality is at best limiting your market and at worst dooming your code to obsolescence when the next big language comes around. So as much as I instinctively like Rust, I think I'll be sticking with C.
- kibwen 14y agoI'm not sure if this will alleviate all of your issues, but eventually the goal is to allow Rust programs to forgo the runtime entirely. An old comment from pcwalton (sorry, I've lost the link): "I'd like to see a 'runtime-less Rust' myself, because it'd be great if we could implement the Rust runtime in Rust. This might be useful for other things too, such as drivers or libraries to be embedded into other software. (The latter is obviously of interest to us at Mozilla.) Rust programs compiled in this mode would disable the task system, would be vulnerable to stack overflow (although we might be able to mitigate that with guard pages), and would require extra work to avoid leaks, but would be able to run without a runtime. "If anyone is interested in this project, I'd be happy to talk more about it -- we have a ton of stuff on our plate at the moment, so we aren't working on it right now, but I'd be thrilled if anyone was interested and could help." See also the remark by brson further down in this thread: http://news.ycombinator.com/item?id=4578044 http://news.ycombinator.com/item?id=4578044
- EternalFury 14y agoAnother language that adopts a different syntax for the sake of adopting a different syntax. If you are going to ask me to adopt a different syntax, you'd better have a good reason for it, because I can do everything I need to do with the languages I already have.
- bryanlarsen 14y agoYou can do more in assembly language than you can in any other language. That doesn't make it a good language to program in except in very special cases.
- ww520 14y agoRust looks very interesting. I downloaded it and tried it out on Windows but got "libgcc dll missing" error. They really need to statically link in the MingGW runtime to make it easier to run.
- pcwalton 14y agoThere is a bug on doing that, but keep in mind that bundling libgcc would only make Rust error out with a more informative error. Because Rust uses the system linker, you need MinGW installed to use Rust.
- marshray 14y agoSomeone ought to mention the similarities between Rust and Scala. These are two really great languages, IMHO.
- deleted 14y ago[deleted]
- shmerl 14y agoRust indeed looks very promising. I hope it'll pick up usage beyond Mozilla internal projects.
- BrandonM 14y agoFrom the end of the last post in the series (http://winningraceconditions.blogspot.com/2012/09/rust-4-typesafe-shared-mutable-state.html http://winningraceconditions.blogspot.com/2012/09/rust-4-typ...): let arc = RWARC(some_data); for num_cpus().times { let arc2 = clone(&arc); do task::spawn |move arc2| { process_data(arc2); // might read, write, whatever } } let modified_data = unwrap(move arc); // blocks on all child tasks at once // do more of the algorithm, etc. This is a nice fork/join pattern, allowing implementations with minimal copying and reasonable safety. If this is indicative of design decisions made by the creators of Rust, I think it looks pretty good.
- philhippus 14y ago>Stack-allocated data lets you often avoid dynamic allocation overhead and garbage collection (even closures can sometimes be entirely on the stack). There's an entire website named after the results of over-using the stack. Smart pointers and move semantics in C++11 completely smoke GC and memory management everywhere else. While I'm sure Rust is a nice language and I applaud its innovation,there's already a perfectly good wheel.
- argv_empty 14y agoSmart pointers and move semantics in C++11 completely smoke GC and memory management everywhere else. Aside from having to enforce proper pointer usage at runtime, whereas Rust does it at compile time.
- deleted 14y ago[deleted]
- pcwalton 14y agoRust enforces that you use its smart pointers safely, eliminating a huge class of errors and exploitable security vulnerabilities. C++ doesn't.