Rust is a modern programming language created and developed at Mozilla Research in 2010 by its namesake author, Brendan Eich. Like most languages, Rust has many built-in primitives that allow you to make your programs more efficient: loops, conditional statements, string manipulation, etc. However, Rust also improves upon these primitives with a modern type system that guarantees Memory Safety and an innovative design inspired by Haskell’s approach to concurrency without sacrificing performance. The result is a fast and safe language that can be used alongside any other high-level language to build highly performant applications that run on any platform without compromising security or scalability.

Rust is a strongly-typed language, which means that all of your code must be declared before it can be evaluated. This helps to guarantee memory safety and prevents accidental errors from happening at runtime. Rust also uses a garbage collector, which means that you do not need to manually free (or delete) memory. Instead, the garbage collector will automatically take care of this for you. In addition, Rust has a modern type system that guarantees Memory Safety and provides a set of built-in types that make it possible to create safe, type-safe programs in just one line of code.

Rust has been shown to be faster than C++, but it also uses less memory due to its functional style. This is because Rust does not allocate memory on the heap, but instead uses pointers to store data. In this way, the garbage collector can automatically free all allocated memory when it is no longer needed. Rust’s type system has been designed to work with any other programming language and be used as a safe alternative to C++.

Updating the System

The first step is to update your system. It is strongly recommended that you always keep your system up to date. This way, you will always have the latest security updates installed and your system will be more stable. To do this, open a terminal (Ctrl+Alt+T) and type: sudo apt update && sudo apt upgrade This will update your list of available packages and then install all of the updated packages.

sudo apt update -y
sudo apt upgrade -y

After the system is updated, we need to install some important packages which are required for Rust. Run the following commands to install the curl, build-essential, gcc, and make package.

sudo apt install curl build-essential gcc make -y

curl is an easy way to download and upload files and web pages. build-essential is a meta-package that includes many development tools needed to compile programs. gcc is the GNU Compiler Collection, which is used to compile Rust code. make is a utility that can be used to create, compile, and link programs. The -y option makes curl download and installs the file automatically. If you leave off the -y option, you will be prompted to answer “yes” or “no” each time curl asks whether you want to download and install a new package.

Installing Rust

Now that our system is up to date and has all of the necessary packages installed, it’s time to install Rust.

There are several ways to install Rust. You can use the binary, you can use one of the pre-built Debian packages, or you can compile it from source. We are going to install Rust using the ready-made installation script from the developer.

By using the installation script, we will get an already configured system that is ready to go right out of the box. This will save us a lot of time and trouble.

Run the following command to download the Rust installation script.

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

By using curl, we can download and install the Rust installation script. The –proto option allows you to specify the protocol that you want curl to support. https is the protocol that is used to download the Rust installation script. The –tlsv1_2 option specifies the ciphers that should be used when making a connection. This is required so that curl can communicate with HTTPS servers such as rustup.rs.

With the –tlsv1.2 flag, we ensure that the server we are connecting to uses the latest TLSv1.2 protocol. The -sS option tells curl that this is a silent download and that it should not produce a progress bar or any other output. The final flag -f forces curl to continue the download even if it detects an unknown error.

The installation script is downloaded from https://sh.rustup.rs and executed as a shell script. The sh argument tells the shell that it should be executed as a shell script.

You will be asked to choose the installation options. It’s recommended to choose the default options unless you have a good reason not to. Type 1 and press Enter.

Install Ruts using Rustup

The installation process may take a while (about 10-15 minutes) depending on your internet connection speed and your server loads. When the installation is finished, you will be presented with a summary of the installation process.

Rust has been installed successfully

Finally, run the rustc -V command to verify that Rust has been installed successfully. If you see an output similar to the one below, then everything is working correctly.

Rustc is the Rust compiler, and it’s used to compile code written in the Rust programming language. The -V flag tells rustc to print out some basic information about itself.

The version number displayed in the output is the current stable Rust release. The unstable release of Rust also has a version number. However, it’s prefixed with an uppercase U.

However, we are not done yet. You need to run two more commands before you can start using Rust.

Run the source ~/.profile command to update the system environment variables. We use the source command to execute a shell script, which modifies the system environment variables. We do this so that the PATH system variable is updated, which is necessary for the Rust compiler to work correctly.

source ~/.profile

System environment variables are used to store values that are used by the system. Some common environment variables are:

  • HOME : This variable contains your home directory.
  • PATH : This variable contains a list of directories that are searched when you try to run a program.
  • TERM : This variable contains information about the terminal type.

Next, run the source ~/.cargo/env command to update the cargo environment variables. Rust uses Cargo to manage packages. The cargo variables are used by the Rust compiler to locate where the package binaries are located.

source ~/.cargo/env

Finally, run the rustc -V command to verify that Rust has been installed successfully. If you see an output similar to the one below, then everything is working correctly.

Rustc is the Rust compiler, and it’s used to compile code written in the Rust programming language. The -V flag tells rustc to print out some basic information about itself.

rustc -V

The version number displayed in the output is the current stable Rust release. The unstable release of Rust also has a version number. However, it’s prefixed with an uppercase U .

Testing Your Rust Installation

Now that you have installed Rust, it’s time to test your installation. We are going to test your installation by running a simple program called “hello world”. This program will print a message that says “hello world” to the screen.

The hello world program is a computer program that outputs or displays the words “hello world” to the user. This phrase is used as the output to test a new compiler, programming language, operating system, or piece of software. It is a simple program used to make sure that everything works properly on a new system.

The goal of this demo is to introduce you to simple concepts first and then you can gradually move into more advanced topics.

First, we will need to create a new directory to store the source code of the hello world program. Run the mkdir hello command to create a new directory called hello.

mkdir hello

Next, use cd to enter the newly created directory.

cd hello

Next, run the sudo nano hello-rust.rs command to open the nano text editor and create a new file called hello-rust.rs.

sudo nano hello-rust.rs

The hello-rust.rs file is the source code of the “hello world” program. Copy and paste the following code to this file.

fn main() {
  println!("Hello, This is a text provided by Vitux");
}

fn main is the main function of the “hello world” program. fn main is equivalent to the main function in C and other programming languages. However, unlike other languages, the function name is always lowercase.

The println! macro prints text to the screen. The string between the two percent signs (Hello, This is a text provided by Vitux) will be printed to the screen.

After you’ve pasted the code, press CTRL+X to exit the nano editor. Press Y for yes and then press ENTER to save the file.

Now that we have the “hello world” program in our hello-rust.rs file, it’s time to compile this program into a binary. Run the rustc hello-rust.rs command to compile the source code into a binary file.

rustc hello-rust.rs

The binary file is a program that the computer understands and can run. It contains all the code required to run the program. Your source code needs to be converted into a more primitive language before it can be read by the computer. This conversion is called compiling.

Once the rustc hello-rust.rs has been successfully compiled, run the ls command to display all the files in the directory. You should now see a new file called hello-rust inside your hello directory.

ls

Rust test script

Finally, run the./hello-rust command to execute the program.

./hello-rust

This command will run the “hello world” program and it should print out a string to the screen. Congratulations! You’ve successfully installed and run your first Rust program on Debian 11.

Hello World in Rust

Conclusion

We hope that this tutorial helps you install Rust on your Debian 11 machine as well as help you to get a good understanding of how the Rust compiler works. For more information about Rust, we recommend that you visit http://www.rust-lang.org/

How to Install Rust Programming Language on Ubuntu 20.04

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn