Have you heard of Rust (I’m not talking about iron oxide)? It’s a new systems language being baked up by the Mozilla Foundation. Currently it is very new and unstable (in the sense that it will change a LOT by the time is reaches version 1). It looks very interesting and is well worth a look.

From my VERY limited knowledge of the language, it looks like C, Go, and Ruby had a child. However, I’m sure my opinion of this will change greatly as I learn more about the language. All I can really say for sure is that it looks like it’s going to be a great/fun language to learn. How could it not with a feature list consisting of:

  • pre-compiled (C/C++ compatible)
  • static typing, with type inference
  • simple concurrency model
  • lambdas everywhere (the way it should be)
  • immutable by default
  • move semantics (good explanation here)



Compiling

Okay, if we’re going to play around with the language then we’ll at least need to compile it and get it up and running. For me, I had just setup a new Fedora 17 instance (no updates at this point) and to compile only required that I do:

1 # get all of the necessary tools to build
2 sudo yum install gcc-c++ llvm llvm-devel perl wget
3  
4 # following rust-lang.org's instructions
5 wget http://dl.rust-lang.org/dist/rust-0.3.tar.gz
6 tar -xzf rust-0.3.tar.gz
7 cd rust-0.3
8 ./configure
9 make -j 4 && make install

Do note that I use the -j option for make to speed things up on my computer. I’d recommend to change the value of -j to however many cores your computer has available. You might also need to run make install as sudo if you do not have the necessary permissions.



Hello Rust

As is tradition, you have to force your newly compiled interpreter/compiler to introduce itself to the world. And, in Rust, it’s just about as boring as it is in any other language:

1 fn main(args: ~[str]) {
2     io::println("hello world from '" + args[0] + "'!");
3 }

A slightly more exciting example (shamelessly stolen from rust-lang.org’s tutorial), a parallel game of rock-paper-scissors:

 1 use std;
 2  
 3 import comm::{listen, methods};
 4 import task::spawn;
 5 import iter::repeat;
 6 import rand::{seeded_rng, seed};
 7 import uint::range;
 8 import io::println;
 9  
10 fn main() {
11     // Open a channel to receive game results
12     do listen |result_from_game| {
13  
14         let times = 10;
15         let player1 = "graydon";
16         let player2 = "patrick";
17  
18         for repeat(times) {
19             // Start another task to play the game
20             do spawn |copy player1, copy player2| {
21                 let outcome = play_game(player1, player2);
22                 result_from_game.send(outcome);
23             }
24         }
25  
26         // Report the results as the games complete
27         for range(0, times) |round| {
28             let winner = result_from_game.recv();
29             println(#fmt("%s wins round #%u", winner, round));
30         }
31     }
32  
33     fn play_game(player1: str, player2: str) -> str {
34  
35         // Our rock/paper/scissors types
36         enum gesture {
37             rock, paper, scissors
38         }
39  
40         let rng = seeded_rng(seed());
41         // A small inline function for picking an RPS gesture
42         let pick = || (~[rock, paper, scissors])[rng.gen_uint() % 3];
43  
44         // Pick two gestures and decide the result
45         alt (pick(), pick()) {
46             (rock, scissors) | (paper, rock) | (scissors, paper) { copy player1 }
47             (scissors, rock) | (rock, paper) | (paper, scissors) { copy player2 }
48             _ { "tie" }
49         }
50     }
51 }

I Want More!

If you’re like me, then you think that Rust could really turn out to be something fun and awesome (I really recommend heading on over to rust-lang.org to learn something more substantial)! But, I’m not here to give a thorough walk-through of the language. I’m more or less just giving a shout-out to what could really be a very interesting systems-programming language.