Introduction

I often see on forums that the Rust language is great but not adapted for beginners as a first language. I disagree. Personally, I have discovered programming with the C language and I do not think that my experience would have been that much different if it was Rust. However, there are few tutorials aimed at total beginners. That’s why I will try to write a serie of posts to make an introduction to the Rust language requiring no knowledge of programming. Please note that I am not a Rust expert myself so I welcome any critic of my code and explanations!

What is programming?

If you are reading this, chances are that you are interested in learning the Rust programming language. But what is programming? Here is the definition from wikipedia:

Computer programming is the process of designing and building an executable computer program for accomplishing a specific computing task.

A computer is a really really stupid machine. If you want it to do any task you will have to specify exactly what you want. There is no place for ambiguity with a computer, you have to always be really clear on what you want it to do. This is why you cannot use English, Japanese or French language to “talk” with a computer. Human languages are not perfectly clear and can have some uncertainty in the meaning of their sentences, however, it works well for us because we are able to infer the true meaning from the context.

At its core, a computer only understands Machine code which is a sequence of 0 and 1 named binary code. However, even if possible, it would be incredibly difficult to program using this language. Instead, we created other programming languages, closer to human languages but farther from the computer. Rust is one them! When you are programming in Rust, your code will then be transformed into machine code by a compiler.

Installing Rust and compiling our first program

Installation

Ok so how do we begin? Rust installation steps will vary depending on your operating system. You can find the instructions here. I will now assume that you are able to use the command line.

The Rust language is constantly evolving, so the first thing you should do is to update the rust compiler. It is as simple as typing the following:

rustup update

Rust is divided into versions and editions. For now I advice you to not care at all about it. If you want you can update the compiler every month but it is not that much necessary for beginners.

Our first program

So let’s really begin! When you want to create a new program, you will need to generate a new project with the cargo program. Cargo is a Rust program that handles for you how to compile your code and many other things. To create a new project just type:

cargo new my_project

Cargo will create a folder named my_project with two files inside: Cargo.toml and main.rs.

my_project_tree

The Rust code you are writing to program will be in the main.rs file. In fact, the .rs extension means that this is a Rust code. If you open it with your favorite editor you will see the following code:

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

It seems strange right? Well don’t worry, you will soon understand it perfectly! But for now, let’s compile it into a program we can execute. To compile your code (the instructions for the computer), you simply need to use cargo:

cargo build

Cargo will do all for you, you just need to wait. When it is finished, you can run your program by again using cargo:

cargo run

And now you should see your computer beginning to talk with you!

Hello, world!

Congratulation, you just wrote your first program! Was not so difficult was it? By the way, you can of course choose any name you want for your project instead of my_project when you are creating a new project using the cargo new command.

Conclusion

We have seen in this post how to install Rust and how to create our first program. However, we did not really do anything. In the next post we will learn how to modify the code and make the computer talk to us!