| by Arround The Web | No comments

Rust Basics Series #1: Create and Run Your First Rust Program

Rust Basics Series #1: Create and Run Your First Rust Program

The Rust programming language is one of the fastest adopted systems programming languages by developers and tech companies. It is also voted as one of the most loved programming languages by developers who use it on a daily basis. Rust has been getting this love for seven consecutive years now!

It is so popular that there are now two big efforts being carried out in the Linux ecosystem:

And that is just in the Linux ecosystem. Android's Bluetooth implementation Gabeldorsche is now written in Rust.

Do you see the rising popularity of Rust? You would probably like to learn coding in Rust.

Why should you consider Rust over other programming languages?

Rust is a programming language that has an extremely strict type system. As a result, you are "forced" to not write bad code in the first place (well, usually).

The Rust programming language has the following "goals":

  1. Speed: Rust's binaries are as fast as C binaries, sometimes outpacing C++ binaries!
  2. Memory safety: Rust has a huge emphasis on memory safety.
  3. Concurrency: Focusing on memory safety eliminates a lot of race condition-like scenarios and helps you introduce concurrency in your program.

Following are a few errors mistakes one might make in languages like C/C++ (but not with Rust):

  • Use after free
  • Double free
  • Accessing out-of-bound values
  • Using NULL
  • Inappropriate pointer arithmetic and/or access
  • Use of uninitialized variable(s)
  • Thread-unsafe multi-threading

Have a look at the issues caused by such issues at major corporations such as Apple, Microsoft, Google, 0day etc,

Now that you know why one might want to choose the Rust programming language over any other one, let's start with the Rust language tutorial series!

Intended audience

For the love of Rust, I am writing this series of Rust tutorials to help you get acquainted with the concept of Rust programming.

This tutorial series is intended for folks already familiar with programming languages like C and C++. I assume you know basic terms like variables, functions, loops, etc.

The only prerequisites that I ask from you are your time and some effort.

Installing the Rust compiler

I would prefer that you have the Rust compiler installed locally. You can do so by running the following command:

curl --proto '=https' --tlsv1.3 -sSf https://sh.rustup.rs | sh
Rust Basics Series #1: Create and Run Your First Rust Program
Installing Rust

Apart from the Rust Compiler, I also recommend installing a few more tools that will help you in the development process:

rustup component add rust-src rust-analyzer rust-analysis
💡
If you do not wish to install the Rust compiler, no worries. You can run the Rust code directly in your browser! Just head over to the Rust Playgrounds website and paste the code discussed here.

Hello Rust!

Since Dennis Ritchie and Brian Kernighan introduced the C programming language with the "Hello world" program, it has become a custom in the UNIX world to do so with any new programming language you learn.

So let's write our Hello World program in Rust as well.

I will create a project directory called learn-rust-its-foss in my home directory. In there, I create another directory called hello-world. Inside that, I will create a main.rs file:

// this code outputs the text
// "Hello world!" to `stdout`

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

📋
Just like C, C++ and Java source files have the extensions .c, .cpp and .java respectively, the Rust source files have the .rs file extension.

As a C/C++ programmer, you might have used gcc on Linux, clang on macOS and MSVC on Windows. But to compile Rust code, the language creators themselves provide an official rustc compiler.

Running a Rust program is the same as executing C/C++ program. You compile the code to get the executable file and then run this executable to run the code.

$ ls
main.rs

$ rustc main.rs

$ ls
main  main.rs

$ ./main
Hello world!

Nice!

Deciphering Rust code

Now that you wrote, compiled and ran your first ever Rust program, let's de-structure the "Hello world" code and understand each part.

fn main() {
}

The fn keyword is used to declare a function in Rust. Following it, main is the name of this particular function that was declared. Like many compiled programming languages, the main is a special function used as your program's entry point.

Any code written inside the main function (between the curly brackets { }) gets executed upon program start-up.

println macro

Inside the main function, there is one statement:

    println!("Hello world!");

Like the C language's standard library has the printf function, Rust language's standard library has the println macro. A macro is similar to a function but it is distinguished by the exclamation mark. You'll learn about macros and functions later in this series.

The println macro takes in a format string and puts it to the program's output (in our case, that is terminal). Since I wish to output some text instead of a variable, I will enclose the text inside double quotes ("). Finally, I end this statement using a semi-colon to denote the end of the statement.

📋
Just know that anything that looks like a function call but has an exclamation mark (!) before the opening parentheses is a macro in the Rust programming language.

Comments

Rust follows the known commenting style of the C programming language. A single line comment starts with two forward slashes (//) and a multi-line comment is started by /* and ends with */.

// this is a multi-line comment
// but nothing stops me to doing the same
// on the second or third line too!

/*
 * this is a "true" mutli-line comment
 * because it is _fancy_
 */

Conclusion

You just took the first step towards coding in Rust with the Hello World program.

As a practice, perhaps you can write and execute a Rust program that prints "Yes! I did Rust".

In the next part of the series, you'll learn to use variables in your Rust program. Stay tuned!

Share Button

Source: It's FOSS

Leave a Reply