Your first cup of Rust

An introduction to the Rust programming language

Rust is a new language that is being developed by Mozilla. At the time of this writing it is still in version 0.7 and changes to the syntax and feature set can be expected before the 1.0 release.

Development is of course open-source so you can go over to the GitHub page and contribute if you wish.

Rust is being used to write Servo, Mozilla’s new parallel web browser engine. If Servo is successful it could be used to replace Gecko, although at this point this is just speculation on my part.

Why Rust

Rust is a low level systems language designed with memory safety and concurrency features. It is designed to allow easier writing of scalable applications while helping avoid common programming mistakes.

You would consider using Rust in situations where you would consider C, C++ or Go.

A short tour of Rust

Types and variables

Rust is statically typed. Variables can be declared with a type explicitly defined:

// Explicitly typed variable
let blog_post_count: int = 0;
 

We can also omit the type and Rust will infer the type itself. This is similar to using the var keyword in C#.

// Inferred variable type
let comment_count = 0;
 

Here comment_count is still statically typed. Rust will infer the type on it’s own (in this case int).

By default variables will be immutable, meaning their values can’t be changed after they are declared. Immutable variables is a concept often seen in functional programming languages.

You can define a variable to be mutable by using the mut keyword.

Here is an example of trying to modify an immutable variable and the error message that will be raised at compile time:

fn main() {
    // immutable variable
    let answer = 42;

    println(fmt!("The answer is: %d", answer));

    answer = 43;
}


[...] 7:10 error: re-assignment of immutable variable `answer`
[...]:7 answer = 43;

[...]:3:8: 3:14 note: prior assignment occurs here
[...].rs:3 let answer = 42;

error: aborting due to previous error

And here it is with the mut keyword:

fn main() {
    // mutable variable
    let mut answer = 42;

    println(fmt!("The answer is: %d", answer));

    answer = 43;

    println(fmt!("The answer is: %d", answer));
}

Which will net no compile time errors.

Compilation

But how do we compile and run Rust programs?

There are two ways to do this.

First you can run the rustc command which will compile your program into an executable. You can then run this executable from the shell like you would any other.

Secondly you can use rust run which will compile and run the program for you as a convenience. For this we use the rust tool which accepts several commands besides run. You can get more info by simply typing:

rust

Conditionals and loops

Here is an example of conditionals (if/else) and a loop.

fn main() {
  // FizzBuzz with while loop
  let mut number_to_test = 1;

  while number_to_test <= 100 {
    if number_to_test % 3 == 0 && number_to_test % 5 == 0 {
      println("FizzBuzz");
    } else if number_to_test % 3 == 0 {
      println("Fizz");
    } else if number_to_test % 5 == 0 {
      println("Buzz");
    } else {
      println(fmt!("%?", number_to_test));
    }

    number_to_test += 1;
  }
}

Rust also has a loop keyword which is an infinite loop from which you break out with break.

It also has a for loop. for has changed in the past versions and the documentation does not seem to have completely followed suite.

Code Organization

Rust code can be organized in modules.

mod custom_math {
  pub fn ceil(x: float) -> float {
    // re-invent the wheel
    return x;
  }

  pub fn floor(x: float) -> float {
    // re-invent the wheel
    return x;
  }
}

fn main() {
  custom_math::ceil(10.5);
}

You can use the use keyword to import a module in another file.

Conclusion

This was just a short basic syntax introduction but you can find out more with the official tutorial and the official site and learn more about what makes Rust tick.

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