It is said than in order to become reasonably proficient in anything, it takes around 10,000 hours of practice - although Rust is exceptional in many ways, it's not an exception to this particular rule, so let's get our hands dirty...

Installing Rust:

You can install rustc, (the Rust compiler), either by compiling it from source, (it may take up to 30 minutes even on modern hardware), or by installing the latest nightly version, (you may find this script quite useful).
Rust is officially supported on all the major platforms including Linux, OS X and Windows and I hear that it's not hard to get it running on the *BSDs as well, (It has been ported to DragonFlyBSD and it looks like it's in in the FreeBSD ports tree as well).
It is also a good idea to install Cargo, the Rust build and dependency manager, (using the above linked script you'll get both rustc and cargo), but since Cargo is mostly useful for larger projects, you probably won't need it for some time yet.
Consider not installing Rust 0.11 if you can avoid it, (or any other Rust release prior to 1.0 for that matter), since Rust still moves fast enough for any material to be able to afford following the releases, stick with the nighties instead.
Before continuing, ensure that rustc was installed correctly:
$ rustc -v And you should see something similar to:
$ rustc 0.12.0-pre (79e9f14ab 2014-07-27 20:51:16 +0000) if you see that, you're all set, if you don't, go over the installation again or consult the #rust IRC channel. With all that out of the way, (and with your favourite editor ready), let's go write some Rust code!

Hello World:

A wife asks her husband, a software engineer; "Could you please go shopping for me and buy one carton of milk, and if they have eggs, get 6!" A short time later the husband comes back with 6 cartons of milk. The wife asks him, "Why the hell did you buy 6 cartons of milk?" He replied, "They had eggs." via Reddit.

It's time to write the decades-old first program in Rust:

(Execute online!) (What happens when you delete ln from println!?) (Hint: try printing another line.)

Here we see the keyword fn being used to declare a function. Since this simple program only has one function, it needs to be called main() as every Rust program uses the main() function as an entry point for the entire program. As you can see, the main function takes no arguments and returns nothing.
Next, we see a pair of braces: {}, which denote the body of a function. We can write code between the braces.

The function body consists of something called a println! macro, (we'll look at macros in a separate blog post, just note that macro is not the same as a function and that you can recognize a macro from a function because of the exclamation-mark ! at the end), then we have a set of () parentheses and the text Hello Rust! between double-quotation marks "", (they cannot be single-quotation marks, since these are not evaluated), followed by a semicolon, (this feels very familiar if you're coming from a C/C++ or JavaScript background, not so with Python or Ruby - don't worry, you'll get used to it very quickly).
And that's it! This almost felt like a dynamic language, did it not? - we didn't had to #include <iostream> or something like that, (a subset of the standard library is available without importing anything) - it was just a simple function declaration with a string literal printed using a macro on screen.
Note that the type of our "Hello Rust!" string literal is &'static str, (we'll look at this when we'll talk more about strings later on).

Variables a.k.a Variable bindings & constants:

In Rust, variables do not actually vary by default, they're immutable. This makes them almost like constants, only they are limited to their local scope and can of course change values between function calls. For you to actually be able to change the variable, you must use the mut keyword to change an immutable variable to a mutable one:

Go ahead, fix the above program!
(Try to explain to a friend, why the above program didn't work the first time around and how you've fixed it.)

That was easy, but don't misuse mutability - mutable variables are the source of many hard-to-debug bugs in software and this is precisely the reason why they're not mutable by default in Rust - think carefully before making your variables mutable!
(The Rust compiler rustc will try to warn you when you may have overused mutability, but don't rely on this, it's not perfect.)

Variable bindings are flexible and generally easy to master, however there are a few areas where Rust differs from more traditional languages.
For example, if you try something like:

fn main() {
    let y;
    let x = y = 12i;

This will not result in x being equal to y, but rather x having the value of (), which is a special Rust type unit, (it represents "nothing") and unlike in C/C++ it is only a valid value on its own - an int or a String cannot have the value of () assigned to them, (let x: int = (); will not work, but this will let x: () = (); - this is a neat safety feature, just make sure to annotate the types explicitly in cases such as this, otherwise they'll be inferred and you may not catch this problem).

(Special thanks to reddit user p32blo for pointing this out.)

One way to make sure that both x and y have the value 12i is to write it like so:

fn main() {
        let y = 12i;
        let x = if y == 12i {12i} else {0i};
        println!{"{}, {}", x, y};

If you want to mass-bind, you can:

fn main() {
  let (x, y, z) = (15i, "string", 29.99f64);
  let (mut s, a, mut m) = ("can change", "cannot", "can");

This means you can re-write the previous example as:

fn main() {
    let (y, x) = (12i, 12i);

What about constants?
You can create them using the static keyword, followed by the name of the constant and after a colon, it's type, (you have to specify the type here due to type inference being local - languages like Haskell, which do have global type inference advise you to annotate types explicitly anyway, when working in global scope, therefore Rust designers have decided to make it a language feature). Then you can assign a value to it as you'll do with variables.
Constants are usually global, (you can of course have local ones as well, they're not the same as immutable variables!), and are therefore visible across your program, they're immutable by default and you'll have to use the mut keyword and an unsafe{} block to mutate them from within a function).


(Execute online!)

Note that in case of a string literal constant, you'll need to specify it as &'static str, because &str without its associated lifetime is not actually a valid type.


Rust supports all the standard operators:

+, -, *, /, % - Arithmetic operations, namely; addition, subtraction, multiplication, division and reminder, where the multiplication operator is also the raw pointer and dereferencing operator.

||, &&, |, &, ^, !, <<, >> - Logical operators, namely; logical or, logical and, inclusive or, and, exclusive or, logical not, logical left shift and logical right shift. || and && differ from | and & in that they only evaluate the right-hand side of an expression if the left-hand operand haven't already resulted in a satisfactory result - that is they consider the right-hand operand only if the left-hand operand evaluates to false for || and true for &&. The & operator is important in one more way; for references/borrowed pointers.

==, !=, <, >, <=, >= - Comparison operators, namely; is equal to, not equal to, less than, greater than, less or equal to and greater or equal to. It is worth nothing here that => is a pattern matching operator, (see bellow), and is not an equivalent of the >= greater or equal to operator.

box - Together with & and *, (see above), form the set of pointer operators; box specifically creates an owned Box of memory on the heap. Owned boxes have a single, owning pointer associated with them, (remember this example of an owned Box from my first post?).

For even more information on operators and some examples, consider consulting the relevant part of the Manual, (scroll down for binary operators, logical operators etc.).
If you're impatient and wish to learn more about the pointer operators shown here, head over here, although my blog post on Rust pointers will explain everything in more detail.


Rust is a statically typed language and while its advanced type inference makes us forget about types most of the time, we still need to care about them in order to fully understand our programs and be able to fix or optimize them in case the Rust compiler gets the type inference wrong, (happens only occasionally, but it does happen) or when it cannot infer the type at all, (which usually happens when it doesn't have enough content to infer the type from).

Rust has the following primitive numeric types:
u8, i8, u16, i16, u32, i32, u64 and i64 - it also has the aliases int and uint for i64 or i32 and u64 or u32 respectively, (depending on whether the OS is 64 or 32-bit). Most of the operators discussed in the above section, work well with numbers.
There are more complex types available in the standard library.

Numbers of one type can be converted to another using the as keyword, e.g. to convert an int to a f64 type, you'll do:

let x = 10i;
x as f64; // cast an int to a float

Pointers/references, ownership and lifetimes:

We're mainly concerned with two types of pointers in Rust; references and Boxes - there's e.g. the * raw pointer and even more in the standard library, (e.g. reference-counted pointer types), but since these are used less often, we'll not look at them here.

Boxes are of type Box<T> and are created using the box keyword. They used to be called owned pointers or owned boxes, because they could only have a single owner, (to prevent dangling pointers from happening). The box keyword allocates a piece of memory on the heap, (if you're unsure what that is, read this) and returns a pointer to it. Here's an example of a Box:

(Execute online!)

In the above example, b is the single owner of the Box<int> pointing at the integer value 15 in memory.

If we change the owner of a box and then refer to the old one, it will result in an error:

(Execute online!)

The above fails while trying to compile:
error: use of moved value: 'x', as we would expect, since "y" is now the only owner of the memory space containing the integer value 5 - it cannot be owned by both "x" and y at the same time. Note that "y" will let go of its ownership automatically, when it goes out of scope.

So how can we fix the above program? It turns out we can borrow what "y" is pointing to using references, (used to be called borrowed references or borrowed pointers), like so:

(Execute online!)

In the above example, we did the same as previously, only this time we created a reference on line 4 using the & operator, using which we borrowed what "y" owns and can therefore print it successfully.

When we borrow however, the original owner has certain restrictions placed upon it, (like it cannot change the fields in a borrowed data structure) and the're only removed when the borrowed reference returns, by going out of scope.
It is also possible to return a reference explicitly, as we shall see in a separate blog post on pointers/references, ownership and lifetimes.

Lifetime simply refer to how long will a pointer/reference to an object will live, (be used) and thus for how long the Rust compiler needs to ensure that an object pointed to/referenced won't change/move or be deleted from underneath the still active pointer. This is how you avoid dangling pointers in Rust. Having a pointer to a freed space on the heap is a compiler error.
When pointers get out of scope, memory is freed.
With references, a borrowed reference is returned once it gets out of scope.
Remember how we talked about a string literal having the type &'static str in our "Hello Rust!" program?, (see above) - well, you'll learn more about the &str part when we'll discuss strings, (bellow), however the 'static part refers to the lifetime of a string literal - it simply means that your string will live as long as your program's process does.

Pointers/references, lifetimes and ownership in Rust are the key to its safety guarantees, and so they deserve their own blog post altogether, just note that although they're quite complex, the information from this blog post will come very handy once we do get to discuss pointers, references, lifetimes and ownership in more detail in a separate post.


There are two basic types of strings that we are interested in, (strings in Rust are more complex than you may be used to, this is because of its memory-safety guarantees - don't worry, we'll go trough them), namely the &str and String types, (although as we shall see, &str can rarely be used as a type on its own).

&str, should be used when you need to borrow a string, (most of the time - remember our debate on references and borrowing above?). &str, (with the 'static lifetime), is what string literals use, although you have the freedom to explicitly specify that you want a String.

String, should be used when you need to own a string, (remember our debate on Boxes above?) - it is a Rust idiom to avoid using String and prefer &str unless there's an explicit reason to use String.

You can convert a &str to a String with the to_string() method and a String to a &str with the as_slice() method.

Example of using the to_string() method:

(Execute online!)


One of the most fundamental aspects of any programming language are the control structures it provides and Rust is no exception with its if/else set of expressions as well as the well known for/while loop constructs, but it also has a more powerful control structure up its sleeves in the form of pattern matching.

if/else - works as you would expect, only it does not require parentheses around the expression and requires braces around the expression's body:

(Execute online!) (Any other interesting languages to add? :-))

As can be seen in the example above, one can combine if and else into else if to allow for multiple possibilities, just like they would in C++. Unfortunately, there's no single elsif or elif statement to achieve the same, so for you Rubyists/Pythonistas out there, else if will have to suffice.

for/while - as with the if/else set, for and while create the standard combo of looping constructs in Rust as they do in any other, decent programming language, (I'm looking at your single-for loop Go!), there's not much new that Rust brings to the table here, if you're coming from a Python background - if you're coming from a C/C++ background, some of this will be new to you:

(Execute online!) (Have you noticed that it is a nested loop?)

We can see in our example above, that Rust provides some high-level looping constructs, like in range, (which would feel very familiar to Python programmers). We can also use the _ throwaway "variable", if we're not interested in any individual values.

Another great feature of Rust is its ability to match particular behaviour to patterns. Pattern matching is one of the most powerful and flexible features of Rust. It works by matching a pattern, (who would've guessed this one?), specified on the left-hand side of a pattern-matching expression with whatever is on the right-hand side of the expression, that is whatever is to the right of the => matching operator, depending on the value of a variable, e.g:

(Execute online!) (modify x's value, what happens when x is an integer?)

In the example above, we'll output a string, depending on which value pattern the x variable fits into. If you made x an integer in our example above, (e.g. you gave x the value of 3), then you've noticed that this will not compile - pattern matching works with compatible types only, no magic auto-casting.

Data structures:

The two most common types of complex data structures you'll likely to use in Rust are tuples and structs, each of these serve a different purpose and can be used to build even more complex, custom data types.
We'll explain both of these in turn:

tuples - lists of fixed order and size, created similarly to variable bindings:


(Execute online!)

We can see that tuples are not limited to one type and that individual values in a tuple can be accessed trough a process called destructuring, which assigns each value to a variable.

structs are a data type meant for object-oriented programming. They are similar to tuples, however their individual members are named and we can access each individual member, (or field), without destructuring.


(Execute online!)

We can access the individual fields in a struct by name using the dot notation, however we cannot do something like println!("{}", today); to print the whole struct, (as we can do with a tuple).

User input:

To capture input from a user, you'll want to use the io module from the standard library, (std), which includes the stdin() function. This function handles whatever is the correct standard input, (depending on the situation that Rust is running in).
You'll then want to use the read_line() function as shown in the following example:

(You'll have to compile this on your own machine as Rust Playpen does not support input.)

In the above example, we use the read_line() function to read what the use typed into the standard input, (console in this case).
ok() is similar to match, (see above), only it does not require for us to handle every case and assumes valid value - it does however provide us with expect(), which can be used to handle an unusual situation, (such as not being able to read user input).

Printing the resulting input into the standard output, (our console here), is pretty standard except we want to print some text right after our input and having the newline character automatically inserted into it is therefore inconvenient for us. We thus use the as_slice() method to convert our input String into a &str, (remember this method from our section on strings?) ,as the method trim_right_chars()we need next, does not work on a String.
As can be guessed from its name, this method deletes specified characters to the right in a &str - in this case we want to delete the auto-appended newline character from whatever the user putted in, like so:
.trim_right_chars('\n') - a less explicit equivalent would be .trim(), (which is more akin to chomp or split() from Ruby or Python respectively).


Functions are the essential building blocks of a program, as they make things happen - take for instance this simple function:

fn add(x: int, y: int) -> int {
    x + y

It takes two integer values, adds them together and returns a resulting integer, (where -> is the "return value" operator).
Rust functions follow the C/C++ tradition of returning one value per function, although Go-style multiple return values are possible with a tuple, (which is still a single value as such, it just contains multiple items).

Here's a full program utilizing the add(x: int, y: int) -> int function:

There's quite a lot going on here, so lets walk trough this:
First, we see the add function we already explained above, northing new here.
Next, we need user input for our two operands a and b resp. x and y, (for details on that, see the User input section above), however what's interesting here is that what the read_line() function gave us isn't actually all that useful. It captured a String - this means that we cannot use it directly as our function takes only integers.
The solution here is to cast, (convert), our String to an int, however this presents another challenge;
What if the user doesn't actually enter a number, (so we'll not end up with a String like "5", but with a String like e.g. "Rust" instead?).
One can solve this problem with the Option<T> type and a match statement. Option<int> can "make sure" that there's actually a valid integer to work with and then strip it to an int, (or throw an error), - this makes sure that our program is type safe.
Having an int, we can match the a, b parameters to the x, y arguments by calling our add function with these arguments and it returns an int result as expected.

Where to go next:

I'll continue to write about my experience learning Rust in the weeks moving forward, however there's other pieces of documentation available you may want to check out too, namely; Rust for Rubyists, Rust for C++ programmers, The Rust Tutorial,(being slowly replaced by the Rust Guide), stdlib documentation and the Rust Reference Manual.
There's also a great set of Rust Guides on various topics here, a great set of Rust's usage examples here and of course there's the great folks on Mozilla's IRC network, (the #rust channel).

NOTE: Rust is a complex language and I'm still learning it, as a result, some of what's written here may be incorrect. I'll correct any mistakes as soon as I am made aware of them.