Mozilla's Rust programming language at critical stage | Opensource.com

Mozilla's Rust programming language at critical stage

Posted 03 Jul 2014 by 

Rating: 
(7 votes)
Image by : 

opensource.com

submit to reddit

At this year's Great Wide Open conference, Steve Klabnik gave a talk about Mozilla's Rust programming language. Klabnik previously authored an introductory Rust tutorial entitled Rust for Rubyists, and this talk serves a similar purpose. However, instead of being Ruby focused, this talk was aimed at programmers in general. Hence the talk's title: Rust for $LANGUAGE-ists.

Klabnik's talk consists of three parts. First, he presents the origin of the Rust language and some information about where the language is headed. The second covers code examples. The examples range from the ubiquitous "Hello World" to more complex examples that highlight some of the features that set Rust apart from other languages. The final section provides resources for learning more about Rust.

Below, I will cover the some of the highlights from Steve Klabnik's talk, but I highly recommend that you watch his talk on YouTube or read the slides from his presentation. I learned a lot from his presentation, and I think you will too.

What is Rust?

Rust is a systems language that is an alternative to C++. In theory, you can use Rust for any programming project that you would use C++ for. Rust's advantages over C++ are that Rust is designed from the ground up to be "safe, concurrent, and fast."

Originally, Rust was a personal project of Graydon Hoare, who works at Mozilla. When Mozilla wanted to start a Research & Development department, Rust became its first project. Mozilla writes a lot of C++ code and has to deal with C++'s drawbacks. For example, all three exploits found in Firefox during the last Pwn2Own competition were because of C++ (e.g., mismanaged pointers). In Rust, those kinds of programming errors won't even compile.

Hoare is no longer heading the development of Rust. He has moved on to other projects. Rust has reached a mature enough stage of development that the project can continue without its original developer. That does not mean that Rust is a fully mature project. The current release is version 0.10 and new releases do break compatibility. Code examples and documentation from earlier releases might not work for the newest release, so there can be some confusion for new users. Version 1.0 is coming soon and that release will stabilize everything.

Rust is at a crucial point in its life cycle. It is not fully mature, so some people might be put off from using it, but it needs people working with the language to improve it before the 1.0 release. The project needs many eyes from people with different perspectives to make sure that the Rust 1.0 release is as good as it can be.

Code examples

Hello World in Rust:

use std::io::println;

fn main() {
    println("Hello, world");
}

This is the standard "Hello World" application written in Rust. It should look familiar enough to anyone with experience with any of the C-style, curly brace languages. The first line imports the println function. Main is declared with fn instead of int, public static void, or the like. Rust is slightly different from what C/C++/C#/Java programmers are used to but close enough that it is not completely foreign.

Segfaults:

int main(void)
{
    char *s = "hello world";
    *s = 'H';    
}

This C++ code will compile with a warning, but it will segfault when run. Rust is designed to stop that from happening, as the following example shows.

fn main() {
    let s = &"hello world";
    *s = "H";
}

segfault.rs:3:4 3:6 error: type &str cannot be dereferenced
sefault.rs:3    *s = "H";
                ^~
error: aborting due to previous error
task 'rustc' failed at 'explicit failure', ...

Instead of compiling, Rust prints out the above error message. This is one of Rust's strengths. Its focus on safety means that it is designed to stop errors like the one in the above C++ example from making it into compiled code.

A more complex example:

fn main() {
    let nums = [1, 2];
    let noms = ["Tim", "Eston", "Aaron", "Ben"];
 
    let mut odds = nums.iter().map(|&x| x * 2 - 1);
 
    for num in odds {
        spawn(proc() {
            println!("{:s} says hello from a lightweight
            thread!", noms[num]);
        });
    }
}

This code shows off several features of Rust. Rust uses the let keyword to define variables. The second and third lines of code create a vector of integers (nums) and a vector of strings (noms). Rust supports type inference, so it is not necessary to explicitly state that the vectors contain integers or strings. The next line highlights the fact that variables in Rust are non-mutable by default. If you want to make a variable mutable, you use the mut keyword. The for loop looks like what you would see in more modern languages, like Ruby or Python, but it compiles down to the same machine code as the traditional C++ for loop. The spawn(proc()) block of code demonstrates how Rust can create a thread in which a function can run. And finally, the println! line shows how Rust handles substitution. In the example, {:s} will be replaced with the value from noms[num].

The examples I have covered only scratch the surface. Klabnik's presentation includes many other examples of Rust code. Like I stated above, his presentation is well worth watching. You will learn a lot. Just make sure you download Rust 0.10 if you want to try out the examples in the presentation. The current nightly builds have changed some things, so some of the examples won't work.

Resources for Rust

To conclude his talk, Klabnik suggested the following resources to learn more about Rust. However, he did provide a warning that the official tutorial for Rust is "terrible" and is in the process of being rewritten.

Tutorials:

Discussion:

Check out Rust on GitHub and get involved.

submit to reddit

4 Comments

Jason Baker
Open Source Sensei

Interesting! And it looks like Rust 0.11 just came out yesterday: https://mail.mozilla.org/pipermail/rust-dev/2014-July/010655.html

Vote up!
5
Vote down!
0
holmja
Open Source Sensei

Yes, it did. Which just goes to show how much of a moving target Rust is right now (new releases come out every three months).

I still recommend downloading the 0.10 release to anyone who wants to play with the examples in Steve Klabnik's talk. If they want to explore beyond that, they should obviously switch to version 0.11 or the nightly builds.

Vote up!
4
Vote down!
0
v.podzimek
Newbie

Just want to share a link to a nice overview of Go, Rust and ParaSail:
http://parasail-programming-language.blogspot.cz/2013/04/systems-program...

Vote up!
3
Vote down!
0
holmja
Open Source Sensei

Interesting article. Thanks for sharing.

Vote up!
1
Vote down!
0

Comment now

Joshua Holm

Joshua Holm has been advocating for open source solutions for over fifteen years. Holding Master's degrees in Library & Information Science and Adult & Higher Education, he seeks to combine his educational background with his love of open source in order to provide end-users with open source and open access solutions for their learning, research, and teaching needs. He has seen the power of open source and hopes to build the future the open source way.

Reader favorite