@Mathieu Nivoliez

Getting started with Rust: Presentation and installation

2017-09-09 / 3 minutes.

Hello, today we are going to talk about Rust.

"You are speaking about it quite often, aren't you?"

Indeed, more reason to introduce it properly, right?

"OK, we are listening."

First thing first. Rust is a compiled programming language. Which means that the code you write will be translated to another one that the computer actually understand. This translation will be called executable or library (or, more generically, the artifact). Rust is also a strongly typed language. In other words, variable are of a certain type. We'll get back to it another time. Finally, on the official site, Rust is described as:

Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.

"Segfaults? Threads safety?"

In case you have never practised any "low-level" language such as C or C++, those terms are certainly new for you. I'll try to rapidly define them here, but we will speak more in detail in some other posts.

Segfault, or segmentation fault, is when a program tries to access memory on which it has no right. Just like at your work, you got a desk to work on but you try to use the one of your colleague, and of the same as your colleague doesn't like that, the computer doesn't either. And then the program crash.

And threads... We also call them "lightweight process". They can work in parallel of others. You can think of it as a pile of dishes to wash. You can use more people to wash all the dishes. The risk is in the case where two or more persons try to wash the same plate at the same time.

Rust prevent those issues and you'll see how in the course of these articles.

"OK, but it is still gloomy...."

Nothing more normal, those notions are quite difficult to apprehend, and it will come in time. So don't worry, be happy.

"Fine, do we start?"

Of course! And the first thing we are going to do is to install Rust. To do so, we are going to use the tool Rustup. You will need a terminal from this point on.

If you are using Linux or Mac, this command should be enough:

curl https://sh.rustup.rs -sSf | sh

If you are using Windows, go to the site of Rustup to download the utility and follow its instructions.

Now, time to use Cargo!

"Cargo?"

It's a tool to manage Rust projects. Go into your favourite directory (not the big one little filth, the one for your projects):

For Linux

cd ~/workspace

For Windows

cd %USERPROFILE%\projects

And then

cargo new hello_world --bin
cd hello_world

The flag --bintells to Cargo to make an executable. Without this option, the command will create the configuration for a library.

Inside the folder newly created, you can see a directory named srcwhich will hold up our sources, and a file named Cargo.tomlwhich contains project information:

[package]
name = "hello_world"
version = "0.1.0"
authors = ["Mathieu Nivoliez"]

[dependencies]

Now, take a look to the file main.rsinside src:

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

"How are we supposed to read that?"

I'll help you, do not worry. First, we got a function fn called main(). When called, it will execute the macro println!with "Hello, world!" as parameters, which will print "Hello, world!" in the console.

To compile and execute the program, run cargo run.

Function and macro are big topics, so we will be talking about them later.

In the end, it doesn't .... sorry, in the end we have installed Rust and create a hello world project using Cargo.

See you later in another post on how to getting started with Rust.

-- Mathieu

Sources: The "Book"