This section is also available in the form of film.

1.2 Behind the scenes

One might think one already has a fully functional program, but surely one cannot be great at something one doesn’t understand. For that reason, we’re going to go over every single part of the basic code from the previous section Get-go—although it will be a somewhat brief explanation in preparation for explanations to come.

This is the code from the previous section:

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

What on Earth are all those crazy symbols?

Code blocks

A code block is a wrapper for code, which is nested deeper and deeper inside the program the more code blocks there are contained inside aother. In Rust, we use {} to define one. everything contained in between the {} is held together, and is upon being executed run from top to bottom—all the information contained by it’s run from top to bottom.

This is a code block:

{

}

On the first line, a code block starts. The second line is empty, this is where we put our code, expanding the code block downwards. A code block can be expanded all the way until your computer runs out of memory—pretty much meaning a code block can be as large as infinity itself. Although not entirely true, you never will run out of memory for making huge code blocks. On the third and final line, the same code block beginning on the first line ends.

In our previous code example, you can see we make use of a code block right after fn main().

Main function

The fn in our program, defines a new function. What comes after fn, is the name we will be referring to when calling this function. In this case it’s main, which is the main function of Rust. The main function serves as a starting point for any Rust program. This is the place the compiler will start taking instructions from:

fn main()
{

}

The keyword fn defines a new function where whatever comes after it’s the name of the function—in this case it’s main. The () is a place where you provide a function with arguments. This can in turn be transported from the place calling the function over to the function. The main function doesn’t take any arguments, but still requires to have the place for arguments printed out.

In some languages you may be used to retrieving command line parameters from inside the main method’s—or function’s—arguments. This isn’t the case as for Rust. A code block is appended to every function. This code block in particular might be refered to as the function body of main.

Upon any function being called or executed by us or our compiler, whatever instructions are located inside the function body will occur.

Print macros

Print-macros are used all the time in Rust, especially those times you choose to create console applications featuring no graphical UI. Macros aren’t functions, yet acts very similarly to that of functions. Think of macros like functions for now. Print-macros in particular are used in order to print out text into our console. In the previous section Get-go, this was used:

println!("Hello, world");

The println! is a macro specified for printing out text into the console appended by a line break. By convention, all Rust macros end on an !. Rust’s macros has a place where parameters may be provided: the parenthesis (). The first parameter inputted into the macro—"Hello, world"—is a string containing the sentence “Hello, world!”. This does by no means have to be “Hello, world!”, it could just as well be “Hello, Lenin!”

At the end of the line in the example above, ; is used to end the line and evaluate the expression. In Rust, semicolons are used to end almost every line as Rust mainly is an express oriented programming language. This means that almost every part of the Rust programming language needs to be—and is—evaluated.

Moreover

Now that you have the basics of Rust in mind, you’ve been set on a path that may take you beyond your wildest dreams. There’s however one more thing that you’re going to need in order to simplify your life a whole lot: Cargo, a program for managing your Rust projects as well as those projects’ dependencies in which they build on.

Cargo comes bundled up together with Rust, which means you most certainly already have it on your computer. To learn how to use it, head on over to the next section Cargo.

Get-go
Cargo