|Rust Hits Stable 1.0 - So What?|
|Written by Mike James|
|Monday, 18 May 2015|
Rust is Mozilla's attempt at creating a system programming language to replace C and C++. It has now reached a stable version 1 and it is time to ask if there is any reason to take notice.
New languages are happening all of the time. Most are experimental and often have a very small group of supporters. Rust is a systems language designed and developed by Mozilla, so you might expect it to have a better chance of sucess. Even so, the chances that any new language will replace C is small.
Rust claims to be fast and safe.
C is fast, but it isn't safe as so many security problems prove. C uses pointers, which are close to the metal, but also very prone to error.
C++ can be made safer with the use of smart pointers, but the cost in terms of speed and complexity is high. Anyway you can argue that C++ isn't a systems programming language because it supports too many high level abstractions like objects to be a C replacement. For example, Linus Torvalds won't have C++ in the Linux kernel and he is probably correct. Would he have Rust? Probably not, no make that certainly not!
Rust is a really nice language with some clever ideas that do make system programming safe. The guiding principle seems to be that the language has to be constructed in such a way that problems can be detected at compile time. The idea goes beyond simple type checking to the point where a program that could have a null pointer error cannot compile. The reason for this approach is that if you check the program for correctness at compile time you can produce a binary that doesn't have to do any more checking and so it can be fast.
The key idea in Rust is ownership. When you declare a variable and set it to some data it owns the data. This means the lifetime of the variable controls the lifetime of the data. When the variable goes out of scope the data is destroyed and the memory returned to the system. Other variables can "borrow" the data owned by a variable, but there are extensive rules on what can be done and what cannot so as to make dangling pointers impossible. In principle, this and other Rust features should make it impossible, or at least difficult, to write a program that access memory it isn't supposed to access.
The same basic idea of letting the compiler do the work is applied to other areas of the language. Abstractions are what make a high-level language high, but they can often be costly in terms of performance. Rust attempts to use "zero-cost abstractions" where an abstraction can be incorporated into the language without imposing a performance cost on the generated code. Rust has lots of high level features - type inference, pattern matching, generics, macros, traits, operator overloading and concurrency. You can even do object-oriented programming in Rust, but there are some things that you have to invent ways of doing for yourself. Functional programming is an easier approach to use in Rust as it supports things like closure, higher order functions, pattern matching and iterators,
So, is Rust a good alternative to C or C++?
It probably isn't a good alternative to C++ which is a sophisticated object-oriented language with lots and lots of abstractions. If you are a good C++ programmer you will probably find Rust restrictive - especially if you want to use OOP.
As an alternative to C is might do better because Rust is a higher-level language and it does seem to trap the silly mistakes you can easily make in C. It also produces an exe file with a minimal runtime, so it does suit the sort of applications that C is used for. However, at the moment at least, Rust might not be fast enough. It uses the LLVM intermediate representation for its output code. This is then compiled to machine code by the LLVM which has the advantage that Rust can target lots of different processors - x86, different flavours of ARM, and so on. However, the LLVM approach isn't as optimized as the GCC approach. What this means is that while Rust code is generally fast, C code can outperform it by factors of more than 2.
C also has another huge advantage - it is very close to the metal. It is best described as a machine independent assembler rather than a high-level language. Its data structures map directly onto memory and programmers often think in low-level terms. This is the reason they make mistakes but, it is also the reason why using C is seen as simple and direct. Rust, on the other hand seems, like a set of rules and complex abstractions that are designed to make it more difficult to get your code to compile!
There is also the small matter that C has been around for a long time and has lots of tools and support. You can learn C and get a job programming and at the moment the same cannot be said with such certainty for Rust. However, Rust is being used in Mozilla's Servo next generation browser engine, which is a big project, and there are even calls for Rust-developed code to be included in Firefox.
There is no doubt that Mozilla's backing helps the language have credibitily, but is it enough?
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
or email your comment to: firstname.lastname@example.org
|Last Updated ( Monday, 06 July 2015 )|