news.

Rust: A language for the future

I can already hear everyone moaning about why another language has been created and how it will differ from the vast selection of languages that already exist. What will make it different and how will it actually improve the current situation where choosing the right language for the job has come down to what language you know the best. Well the truth is it probably won’t but I have found my last 3 weeks with the Rust language rather enjoyable and interesting.

Rust is a new language that was officially unveiled in 2010, and is backed by Mozilla. The main goal of the rust language is to facilitate the creation of large, scalable client-server applications, however the language provides much more than that. Rust is being designed with a major focus on safety, concurrency and memory control, and for these reasons alone it could be the language which makes C++ obsolete.

While C++ is one of the most popular programming languages, and has been used for programming anything from embedded device firmware to video games and even as a hardware specification language, it is inherently unsafe and allows dangling and null pointers to exist within applications. Furthermore, C++ places the responsibility of memory management and clean-up on the programmer which leads to many applications being vulnerable to buffer overflows and memory exploits.

Rust corrects these issues by not allowing the use of dangling pointers and by using its own memory management techniques to allocate and free memory, thus taking the responsibility away from the programmer. While Rust strives to be an efficient language, its performance is not as good as pure C/C++ code, however it is better that the C++ code which offers the same memory security as a Rust application.

Rust is more than just another OO language that tries to overtake C++, but rather a completely new programming paradigm. Rust has borrowed features and principles from many programming languages, such as OCaml, Erlang, C#, C++, Ruby and Lisp, in order to provide a language that anyone can enjoy. Furthermore, Rust offers the programmer the ability to program in a pure-functional, object-oriented, concurrent-actor or imperative-procedural style, which should make it an easy language for most programmers to pick up. Furthermore, the Rust compiler uses LLVM as its backend which should mean that it can play along nicely with many other compiled languages.

Now that you know what Rust is all about let’s look at some code.

HelloWorld.rs

fn main() {

println(“hello, world”);

}

As you can see, Rust follows the C/C++ language style with the use of curly braces to denote the start and end of a method.

To show off the flexibility of Rust have a look at the Fibonacci code below. The first section of code is implemented as a recursive procedural style while the second utilizes Rust’s pattern matching ability to implement the Fibonacci method in a very functional way.

Fib_procedural.rs

fn fib(x: int) -> int {
    if (x == 0) {
        return 0;
    }
    if (x == 1) {
        return 1;
     }
    fib(x - 1) + fib(x - 2)
}
fn main() {
    let ans: int = fib(10);
    io::println( fmt!("%d", ans));
}

 

Fib_procedural.rs

fn fib(vector: (int,int,int)) -> int {
    match vector {
        (_,b,1) => b,
        (a,b,n) => fib( (b, a+b, n-1) )
     }
}
fn main() {
    let args: (int,int,int) = (0,1,10);
    let ans: int = fib(args);
    io::println( fmt!("%d", ans));
}

Rust uses the fn keyword to define a function of the style:

fn some_name( arguments ) -> return_type {}

and uses the let keyword to define variables. In Rust variables may be defined in one of five styles, either as a type and value, such as in the example above, or as a value (as rust supports dynamic typing) or as one of 3 types of pointers.

  • let a: int = some_value;
  • let b = some_value;
  • let c: ~int = some_owned_pointer;
  • let d: &int = some_borrowed_ponter;
  • let e: @int = some_managed_pointer;

For brevity I will not go into each of these pointer types, but they do each have a place and are one of the reasons why Rust is a much safer language than C++.

Furthermore, you may (or may not) have noticed that the second Fibonacci function does not explicitly tell the function what value to return. The reason for this is that Rust interprets functions in a more mathematical sense rather than in a programmatic sense (although both are accepted). The Rust compiler will sort out the understanding of what will be returned based upon Rust pattern matching method. Any value can be returned from a function by simply not following the expression with a semi-colon. This allows for very nice variable instantiation such as:

fn some_method(my_number : int){
   let some_var: ~str = match my_number {
        0     => "zero",
        1 | 2 => "one or two",
        3..10 => "three to ten",
        _     => "something else"
    };
}

The above example shows how Rusts pattern matching can be used to instantiate a variable with out the use of slow and cumbersome if-else or case statements.

While Rust is not the solution to all problems it is certainly a nice language which I believe could have a large impact on the applications of the future.

Disclaimer: Rust is only at release 0.6 and it is hoped that version 1.0 will be released later this year, thus not all the features of a complete language are present but there are certainly enough to build some cool applications.

 

If you want to learn more about Rust see:

www.rust-lang.org

http://static.rust-lang.org/doc/tutorial.html

www.rustforrubyists.com

 

, , , , ,

2 Comments

  1. 1
    Jesse Ruderman on Monday 13 May, 19:47 PM #

    I’d describe “let b = some_value;” as inferred typing rather than dynamic typing. Rust won’t let you assign an int in one branch and a string in another.

    Instead of fully dynamic typing, Rust has enums, generics, and traits.

  2. 2
    Lloyd Hughes on Wednesday 29 May, 10:42 AM #

    Thanks for clearing that up Jesse, that is what I was trying to put across, I guess I just worded it badly.

Leave a comment

Leave a Reply

(required)