Rust Made Easy – Part 1

Edited on 2013-04-03: This page is outdated.

Rust is a prospective language which just had its version 0.1 released, and coming from a background that does not hail from C or C++, learning it seemed quite daunting. Starting off writing Java in college and digesting Python in my free time helped, but just a little. Since I previously had some time on a plane and not really sleeping, I’ve been looking at ways to simplify learning Rust, and hopefully by guiding you, as a reader, along the way will help us share our experiences and enhance learning opportunities for all.

But seriously, what is Rust? You can find a short write-up here, along with things like not allowing null pointers (Yay! no more null crashes). Various other technical bits are way above me now. Nonetheless, follow the instructions to obtain your Rust compiler. In the following examples, save the code snippets in a test.rs file, renaming “test” to whatever you like as long as it still ends with .rs, compile it with the Rust compiler “rustc test.rs” and then execute the compiled file “./test”.

Alright, so let’s dive into Rust. To print a statement, let us understand that we first have to import a standard library or a module, known as std.

use std;

It’s like including iostream.h in C++.

Once we’ve done that, we can now print a statement:

use std;
fn main() {
    std::io::println("Hello World!");
}

to get:

Hello World!

(In Python, it is the equivalent of “print ‘Hello World!”)

In Rust, it is necessary to have a main function. The double colons “::” mean that it is calling println within module io within the std module. It’s similar to System.out.println in Java. Note that semicolons “;” at the end of the lines seem to be necessary most of the time — there are special circumstances where “;” is not needed at the end, but let’s leave that for later.

How about a number? I would like to create a local variable, which is a number, and print it. In this case, we use `let`:

use std;
fn main() {
    let x: int = 8;
    std::io::println("Our number is: " + int::str(x));
}

Two new things here. One is “let x: int = 8;”, which means create a local variable x of the type int, and set its value to 8. Again, there will be situations in the future where you leave out specifying the type, and in those cases the type will be inferred (the compiler will “guess” the type).

Second, note that we cast x to a string before printing it. In Rust, we cannot print a number without first changing its type to a string. This is done by calling str from the int module and passing in x, so we have “int::str(x)”.

You should get the output:

Our number is: 8

You may have noticed that Rust uses shortened keywords, such as “fn” for a function. It also uses “ret” for returning values. We will see more of this as we go along.

Next up, let’s construct a separate function instead of cramming everything up in main, and have it print our favourite number again:

use std;
fn fav_number(n: int) {
    std::io::println("Our number is: " + int::str(n));
}
fn main() {
    let x: int = 8;
    fav_number(x);
}

You may notice that the fav_number function accepts an integer variable n, and this is reflected by “n: int”.

What if we want to pass in our favourite number as a command-line argument? This is done by:

use std;
fn fav_number(n: str) {
    std::io::println("Our number is: " + n);
}
fn main(args: [str]) {
    fav_number(args[1]);
}

Run this with “./test 8” and you should get the same output as above:

Our number is: 8

Remember to run it with a number as an argument – if you leave out the number, you will get an error:

rust: upcall fail ‘bounds check’, test.rs:6
rust: domain main @0x22a1a50 root task failed

Examine this example more closely and you will realize that there is no casting involved. This is because we are taking in the argument (number) as a string, and merely printing it out again.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s